#include <bits/stdc++.h>
int n;
bool s[500][500];
int dist[500][500];
int qx[250000], qy[250000], head, tail;
void bfs(int stx, int sty) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = -1;
    }
  }
  dist[sty][stx] = 0;
  int vx[4] = {0, 1, -1, 0};
  int vy[4] = {1, 0, 0, -1};
  head = tail = 0;
  qx[head] = stx;
  qy[head] = sty;
  tail++;
  while (tail != head) {
    for (int i = 0; i < 4; i++) {
      int nx = qx[head] + vx[i];
      int ny = qy[head] + vy[i];
      if (0 <= nx && nx < n && 0 <= ny && ny < n && dist[ny][nx] == -1) {
        if (!s[ny][nx])
          dist[ny][nx] = dist[qy[head]][qx[head]];
        else
          dist[ny][nx] = dist[qy[head]][qx[head]] + 1;
        qx[tail] = nx;
        qy[tail] = ny;
        tail++;
      }
    }
    head++;
  }
  return;
}
int min(int x, int y) { return x < y ? x : y; }
int main() {
  int hate = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      s[i][j] = true;
    }
  }
  for (int i = 0; i < n * n; i++) {
    int p;
    scanf("%d", &p);
    p--;
    bfs(p % n, p / n);
    int h = 1001001001;
    for (int i = 0; i < n; i++) {
      h = min(h, dist[0][i]);
      h = min(h, dist[n - 1][i]);
      h = min(h, dist[i][0]);
      h = min(h, dist[i][n - 1]);
    }
    hate += h;
    s[p / n][p % n] = false;
  }
  printf("%d\n", hate);
  return 0;
}
