#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 k = 0; k < N2; k++) {
    ans += d[P[k]];
    done[P[k]] = true;
    queue<pair<int, int>> q;
    q.push(make_pair(P[k], d[P[k]]));
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      int k0 = p.first, i0 = k0 / N, j0 = k0 % N, d0 = p.second;
      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(make_pair(nk, d0));
          }
        } else {
          if (d[nk] > d0 + 1) {
            q.push(make_pair(nk, d0 + 1));
          }
        }
      }
    }
  }
  cout << ans << "\n";
}
