#include <bits/stdc++.h>
using namespace std;
const int maxn = 250010;
const int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int n, p[maxn], fa[maxn], mn[maxn];
bool chk[510][510];
int id(int x, int y) { return (x - 1) * n + y; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      fa[id(i, j)] = id(i, j);
      mn[id(i, j)] = min({i - 1, j - 1, n - i, n - j});
    }
  }
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    scanf("%d", &p[i]);
    int x = (p[i] - 1) / n + 1, y = p[i] - (x - 1) * n;
    chk[x][y] = 1;
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (nx < 1 || nx > n || ny < 1 || ny > n) continue;
      int f1 = find(p[i]), f2 = find(id(nx, ny));
      if (f1 == f2) continue;
      mn[f1] = min(mn[f1], mn[f2] + !chk[nx][ny]);
    }
    ans += mn[find(p[i])];
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (nx < 1 || nx > n || ny < 1 || ny > n || !chk[nx][ny]) continue;
      int f1 = find(p[i]), f2 = find(id(nx, ny));
      if (f1 == f2) continue;
      fa[f1] = f2, mn[f2] = min(mn[f2], mn[f1]);
    }
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      if (nx < 1 || nx > n || ny < 1 || ny > n || chk[nx][ny]) continue;
      int f1 = find(p[i]), f2 = find(id(nx, ny));
      if (f1 == f2) continue;
      mn[f2] = min(mn[f2], mn[f1]);
    }
  }
  printf("%d\n", ans);
  return 0;
}
