#include <bits/stdc++.h>
using namespace std;
bitset<250000> M;
bitset<250000> D;
int main() {
  int N, p, np;
  cin >> N;
  vector<int> P(N * N);
  vector<int> nxt = {-1, 1, -N, N};
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
    M.set(i);
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    deque<int> dq;
    D = 0;
    int h = 0;
    dq.emplace_back(P[i] - 1);
    M[P[i] - 1] = 0;
    dq.emplace_back(-1);
    while (!dq.empty()) {
      p = dq.front();
      dq.pop_front();
      if (p == -1) {
        h++;
        dq.emplace_back(-1);
      } else if (p < N || p >= N * (N - 1) || p % N == 0 || p % N == N - 1) {
        break;
      } else {
        for (int nx : nxt) {
          np = p + nx;
          if (D.test(np) == 0) {
            D.set(np);
            if (M.test(np) == 0) {
              dq.emplace_front(np);
            } else {
              dq.emplace_back(np);
            }
          }
        }
      }
    }
    ans += h;
  }
  cout << ans << endl;
}
