#include <bits/stdc++.h>
using namespace std;
vector<int> Vx = {1, 0, -1, 0}, Vy = {0, 1, 0, -1};
int main() {
  int N;
  cin >> N;
  vector<vector<int>> G(N, vector<int>(N)), Mx(N, vector<int>(N, 0));
  vector<int> P(N * N);
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;
    G[p / N][p % N] = i;
    P[i] = p;
  }
  for (int i = 0; i < (N + 1) / 2; i++) {
    for (int j = 0; j < (N + 1) / 2; j++) {
      Mx[i][j] = min(i, j);
      Mx[i][N - j - 1] = min(i, j);
      Mx[N - i - 1][j] = min(i, j);
      Mx[N - i - 1][N - j - 1] = min(i, j);
    }
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    queue<int> Q;
    Q.push(P[i]);
    map<int, int> M;
    M[P[i]] = 0;
    int now = 500;
    while (!Q.empty()) {
      int u = Q.front();
      Q.pop();
      int x = u / N, y = u % N;
      if (x == 0 || x == N - 1 || y == 0 || y == N - 1) {
        now = min(now, M[u]);
        if (now == 0) break;
        continue;
      }
      if (M[u] >= Mx[P[i] / N][P[i] % N]) continue;
      for (int j = 0; j < 4; j++) {
        int v = u + Vx[j] * N + Vy[j], step = 0;
        if (G[x + Vx[j]][y + Vy[j]] > i) step = 1;
        if (!M.count(v)) {
          M[v] = M[u] + step;
          Q.push(v);
        } else if (M[v] > M[u] + step) {
          M[v] = M[u] + step;
          Q.push(v);
        }
      }
    }
    ans += now;
  }
  cout << ans << endl;
  return 0;
}
