#include <bits/stdc++.h>
using namespace std;
const int MID = 505 * 505 * 2;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n, m, i, j, e, x, y, nx, ny, cur, nxt, it, fi, fr, qx[MID * 2], qy[MID * 2],
    a[MID], d[505][505], v[505][505], was[505][505];
long long r;
int main() {
  scanf("%d", &n);
  m = n * n;
  for (i = 1; i <= m; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) {
      d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
      v[i][j] = 1;
    }
  for (i = 1; i <= m; i++) {
    x = (a[i] - 1) / n + 1;
    y = (a[i] - 1) % n + 1;
    v[x][y] = 0;
    for (e = 0; e < 4; e++) {
      nx = x + dx[e];
      ny = y + dy[e];
      if (v[nx][ny] == 0) d[x][y] = min(d[x][y], d[nx][ny]);
    }
    r += d[x][y];
    ++it;
    qx[fi = MID] = x;
    qy[fi] = y;
    for (fr = MID + 1; fi < fr;) {
      x = qx[fi];
      y = qy[fi++];
      if (was[x][y] == it) continue;
      was[x][y] = it;
      for (e = 0; e < 4; e++) {
        nx = x + dx[e];
        ny = y + dy[e];
        nxt = d[x][y] + v[nx][ny];
        if (d[nx][ny] > nxt) {
          d[nx][ny] = nxt;
          if (v[nx][ny]) {
            qx[fr] = nx;
            qy[fr++] = ny;
          } else {
            qx[--fi] = nx;
            qy[fi] = ny;
          }
        }
      }
    }
  }
  printf("%lld\n", r);
  return 0;
}
