#include <bits/stdc++.h>
using namespace std;
const int fx[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, i, j, k, t, w;
int d[505 * 505][2], dis[505][505], bz[505][505], ti[505][505];
int a[505][505], x, y, lim, D[505][505];
long long ans;
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      D[i][j] = min(min(i - 1, j - 1), min(n - i, n - j)), a[i][j] = 1;
  for (i = 1; i <= n * n; i++) {
    scanf("%d", &k), x = (k - 1) % n + 1, y = (k - 1) / n + 1, a[x][y] = 0;
    t = 0, w = 1, d[w][0] = x, d[w][1] = y;
    dis[x][y] = 0, bz[x][y] = 1, ti[x][y] = i;
    lim = D[x][y];
    if (i == 13) printf("");
    while (t < w) {
      t++, x = d[t][0], y = d[t][1];
      for (k = 0; k < 4; k++) {
        int xx = x + fx[k][0], yy = y + fx[k][1];
        if (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
          if (ti[xx][yy] < i) dis[xx][yy] = 2e9, ti[xx][yy] = i;
          if (dis[x][y] + a[xx][yy] < dis[xx][yy]) {
            dis[xx][yy] = dis[x][y] + a[xx][yy];
            if (dis[xx][yy] < lim && !bz[xx][yy]) {
              w++, d[w][0] = xx, d[w][1] = yy, bz[xx][yy] = 1;
              if (dis[xx][yy] + D[xx][yy] < lim)
                lim = min(lim, dis[xx][yy] + D[xx][yy]);
            }
          }
        } else
          lim = min(lim, dis[x][y]);
      }
      bz[x][y] = 0;
    }
    ans += lim;
  }
  printf("%lld", ans);
}
