#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 5e2 + 5;
int cnt;
struct nobe {
  int l, r, x, f, e;
} a[N][N];
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      a[i][j].x = (i + 1) * (j + 1);
      a[i][j].l = j;
      a[i][j].r = n - j - 1;
      a[i][j].f = i;
      a[i][j].e = n - i - 1;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int m;
      cin >> m;
      int p = (m - 1) / n, q = (m - 1) % n;
      for (int k = 0; k < n; k++) {
        if (k < q)
          a[p][k].r--;
        else if (k > q)
          a[p][k].l--;
      }
      for (int k = 0; k < n; k++) {
        if (k < p)
          a[k][q].e--;
        else if (k > p)
          a[k][q].f--;
      }
      cnt += min(min(a[p][q].r, a[p][q].l), min(a[p][q].e, a[p][q].f));
    }
  cout << cnt << endl;
  return 0;
}
