#include <bits/stdc++.h>
using namespace std;
const int N = 505;
template <class T>
void read(T &x) {
  int sgn = 1;
  char ch;
  x = 0;
  for (ch = getchar(); (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') ch = getchar(), sgn = -1;
  for (; '0' <= ch && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  x *= sgn;
}
template <class T>
void write(T x) {
  if (x < 0)
    putchar('-'), write(-x);
  else if (x < 10)
    putchar(x + '0');
  else
    write(x / 10), putchar(x % 10 + '0');
}
int n, p[N * N], inv[N][N], f[N][N], dx[4] = {1, 0, -1, 0},
                                     dy[4] = {0, -1, 0, 1}, ans = 0;
int que[N * N], l = 0, r = 0;
bool vis[N][N];
int main() {
  read(n);
  for (int i = 0; i < n * n; i++) {
    read(p[i]);
    inv[(p[i] - 1) / n][(p[i] - 1) % n] = i;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      f[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
      vis[i][j] = false;
    }
  }
  for (int i = 0; i < n * n; i++) {
    int x = (p[i] - 1) / n, y = (p[i] - 1) % n;
    ans += --f[x][y];
    int sz = n * n + 1;
    l = r = 0, que[0] = x * n + y;
    vis[x][y] = true;
    while (l <= r) {
      int ux = que[l] / n, uy = que[l] % n;
      l = (l + 1) % sz, vis[ux][uy] = false;
      for (int j = 0; j < 4; j++) {
        int tx = ux + dx[j], ty = uy + dy[j];
        if (tx < 0 || tx >= n || ty < 0 || ty >= n) continue;
        if (inv[tx][ty] > i && f[tx][ty] > f[ux][uy] + 1) {
          f[tx][ty] = f[ux][uy] + 1;
          if (!vis[tx][ty]) que[r = (r + 1) % sz] = tx * n + ty;
        }
        if (inv[tx][ty] <= i && f[tx][ty] > f[ux][uy]) {
          f[tx][ty] = f[ux][uy];
          if (!vis[tx][ty]) que[l = (l + sz - 1) % sz] = tx * n + ty;
        }
      }
    }
  }
  write(ans), putchar('\n');
  return 0;
}
