#include <bits/stdc++.h>
using namespace std;
int n, i, j, k, x, y, gg;
long long dis[505][505], ans;
int bz[505][505];
int p[250005];
int list[2500005][3], head, tail, B[505][505];
int fx1[5], fx2[5];
void update(int x, int y) {
  int xx, yy, t;
  head = 0;
  tail = 1;
  memset(B, 0, sizeof(B));
  list[1][1] = x;
  list[1][2] = y;
  B[x][y] = 1;
  while (head < tail) {
    head++;
    x = list[head][1];
    y = list[head][2];
    for (t = 1; t <= 4; t++) {
      xx = x + fx1[t];
      yy = y + fx2[t];
      if (dis[xx][yy] > dis[x][y] + (bz[xx][yy] ^ 1)) {
        dis[xx][yy] = dis[x][y] + (bz[xx][yy] ^ 1);
        if (B[xx][yy] == 0) {
          B[xx][yy] = 1;
          tail++;
          list[tail][1] = xx;
          list[tail][2] = yy;
        }
      }
    }
    B[x][y] = 0;
  }
}
int main() {
  fx1[1] = 1;
  fx1[2] = -1;
  fx1[3] = 0;
  fx1[4] = 0;
  fx2[1] = 0;
  fx2[2] = 0;
  fx2[3] = 1;
  fx2[4] = -1;
  scanf("%d", &n);
  for (i = 1; i <= n * n; i++) scanf("%d", &p[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
    }
  }
  for (i = 1; i <= n * n; i++) {
    x = (p[i] - 1) / n + 1;
    y = (p[i] - 1) % n + 1;
    bz[x][y] = 1;
    dis[x][y]--;
    ans += dis[x][y];
    update(x, y);
  }
  printf("%lld", ans);
  return 0;
}
