#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, n, a;
  cin >> N;
  n = N * N;
  vector<vector<int>> r(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      r.at(i).at(j) = (i * N) + (j + 1);
    }
  }
  long long ans = 0;
  int flg1, flg2, flg3, flg4;
  for (long long i = 0; i < (long long)n; i++) {
    int ip, jp, b;
    cin >> a;
    b = a - 1;
    ip = b / N;
    jp = b % N;
    r.at(ip).at(jp) = 0;
    if (ip == 0 || jp == 0 || ip == (N - 1) || jp == (N - 1)) continue;
    flg1 = 0;
    for (int k = jp; k >= 0; k--) {
      if (r.at(ip).at(k) != 0) {
        flg1++;
      }
    }
    if (!flg1) continue;
    flg2 = 0;
    for (int k = jp; k < N; k++) {
      if (r.at(ip).at(k) != 0) {
        flg2++;
      }
    }
    if (!flg2) continue;
    flg3 = 0;
    for (int k = ip; k >= 0; k--) {
      if (r.at(k).at(jp) != 0) {
        flg3++;
      }
    }
    if (!flg3) continue;
    flg4 = 0;
    for (int k = ip; k < N; k++) {
      if (r.at(k).at(jp) != 0) {
        flg4++;
      }
    }
    if (!flg4) continue;
    ans += (min(flg1, min(flg2, min(flg3, flg4))));
  }
  cout << ans << endl;
  return 0;
}
