#include <bits/stdc++.h>
using namespace std;
int x[250001], pa[250001], r[250001], oc[250001], p, n;
int find(int x) { return pa[x] = pa[x] == x ? x : find(pa[x]); }
void merge(int x1, int x2) {
  int a = find(x1), b = find(x2);
  if (a == b) return;
  if (r[a] < r[b]) {
    pa[a] = b;
    x[b] = min(x[a], x[b]);
  } else {
    pa[b] = a;
    x[a] = min(x[a], x[b]);
    if (r[b] == r[a]) ++r[a];
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    int x1 = i / n;
    int y1 = i % n;
    x[i] = min(x1, min(n - 1 - x1, min(y1, n - 1 - y1)));
    pa[i] = i;
    r[i] = 0;
    oc[i] = 1;
  }
  int dx[4] = {-1, 1, n, -n};
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    cin >> p;
    p = p - 1;
    int pap = find(p);
    oc[p] = 0;
    for (int j = 0; j < 4; j++) {
      int p2 = p + dx[j];
      if (0 <= p2 && p2 < n * n) {
        if (!oc[p2]) {
          merge(p, p2);
          x[p] = min(x[p], x[find(p)]);
        }
      }
    }
    ans += x[p];
  }
  cout << ans;
}
