#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7, INF = (int)1e9;
const int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1};
int main(void) {
  int n, i, j, p, pi, pj, kl, ti, tj, lb;
  long long ans = 0;
  bool st[503][503] = {false};
  scanf("%d", &n);
  for (i = 0; i < n * n; i++) {
    scanf("%d", &p);
    p--;
    pi = p / n;
    pj = p % n;
    lb = INF;
    for (j = 0; j < 4; j++) {
      ti = pi + di[j];
      tj = pj + dj[j];
      kl = 0;
      while (ti >= 0 && ti < n && tj >= 0 && tj < n) {
        if (!st[ti][tj]) {
          kl++;
        }
        ti += di[j];
        tj += dj[j];
      }
      lb = min(lb, kl);
    }
    ans += lb;
    st[pi][pj] = true;
  }
  printf("%lld\n", ans);
  return 0;
}
