#include <bits/stdc++.h>
using namespace std;
int s[15][15], ans, w[15][15], cnt, ans1, row[10][10], col[10][10], cell[10][10];
struct pos {
int x, y, c;
} f[100];
bool cmp (pos x, pos y) {
if (x.c != y.c)
return x.c < y.c;
if (x.x != y.x) {
return x.x < y.x;
}
return x.y < y.y;
}
void dfs (int pi, int sum, int rems) {
if (pi == cnt + 1) {
ans = max(ans, sum);
return;
}
int x = f[pi].x, y = f[pi].y;
if (rems * 9 + sum + 9 <= ans + ans1) return;
for (int i = 1; i < 10; i++) {
if (!row[x][i] && !col[y][i] && !cell[(x - 1) / 3 * 3 + (y + 2) / 3][i]) {
s[x][y] = i;
row[x][i] = col[y][i] = cell[(x - 1) / 3 * 3 + (y + 2) / 3][i] = 1;
dfs(pi + 1, sum + w[x][y] * s[x][y], rems - s[x][y]);
row[x][i] = col[y][i] = cell[(x - 1) / 3 * 3 + (y + 2) / 3][i] = 0;
s[x][y] = 0;
}
}
}
int main () {
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
cin >> s[i][j];
int ti = i, tj = j;
if (i > 5) {
ti = 10 - i;
}
if (j > 5) {
tj = 10 - j;
}
w[i][j] = min(ti, tj) + 5;
if (a[i][j]) {
ans1 += s[i][j] * w[i][j];
}
}
}
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
row[i][s[i][j]] = 1;
col[j][s[i][j]] = 1;
cell[(i - 1) / 3 * 3 + (j + 2) / 3][s[i][j]] = 1;
if (a[i][j]) continue;
int c[15];
fill(c + 1, c + 10, 0);
for (int l = 1; l <= 9; l++) {
c[s[l][j]] = c[s[i][l]] = 1;
}
for (int l = (i - 1) / 3 * 3 + 1; l <= (i - 1) / 3 * 3 + 3; l++) {
for (int k = (j - 1) / 3 * 3 + 1; k <= (j - 1) / 3 * 3 + 3; k++) {
c[s[l][k]] = 1;
}
}
int sum = 0;
for (int i = 1; i < 10; i++) {
if (!c[i]) {
sum++;
}
}
f[++cnt].x = i;
f[cnt].y = j;
f[cnt].c = sum;
}
}
sort(f + 1, f + cnt + 1, cmp);
ans = -1;
dfs(1, 0, 405);
if (ans != -1) {
ans += ans1;
}
cout << ans;
return 0;
}