#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, ans = 0;
  cin >> N;
  int N2 = N * N;
  vector<int> P(N2), d(N2);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int k = i * N + j;
      cin >> P[k];
      P[k]--;
      d[k] = min({i, j, N - 1 - i, N - 1 - j});
    }
  }
  vector<bool> done(N2, false);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int k = i * N + j;
      ans += d[P[k]];
      done[P[k]] = true;
      stack<vector<int>> q;
      q.push({P[k] / N, P[k] % N, d[P[k]]});
      while (!q.empty()) {
        auto p = q.top();
        q.pop();
        int i0 = p[0], j0 = p[1], d0 = p[2], k0 = i0 * N + j0;
        d[k0] = d0;
        int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1};
        for (int l = 0; l < 4; l++) {
          int ni = i0 + di[l], nj = j0 + dj[l], nk = ni * N + nj;
          if (ni < 0 || ni >= N || nj < 0 || nj >= N) continue;
          if (done[k0]) {
            if (d[nk] > d0) {
              q.push({ni, nj, d0});
            }
          } else {
            if (d[nk] > d0 + 1) {
              q.push({ni, nj, d0 + 1});
            }
          }
        }
      }
    }
  }
  cout << ans << "\n";
}
