#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) f = ch == '-', ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
const int N = 505;
int n;
int p[N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int dis[N][N];
int check(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; }
int alive[N][N];
int vis[N][N], tag = 0;
void upd(int x, int y) {
  if (vis[x][y] == tag) return;
  vis[x][y] = tag;
  for (int k = (0); k <= (3); k++) {
    int a = x + dx[k], b = y + dy[k];
    if (check(a, b)) {
      if (dis[x][y] + alive[a][b] < dis[a][b]) {
        dis[a][b] = dis[x][y] + alive[a][b];
        upd(a, b);
      }
    }
  }
}
int main() {
  n = read();
  for (int i = (1); i <= (n * n); i++) p[i] = read();
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (n); j++) {
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
      alive[i][j] = 1;
    }
  int ans = 0;
  for (int i = (1); i <= (n * n); i++) {
    int x = (p[i] - 1) / n + 1;
    int y = (p[i] - 1) % n + 1;
    dis[x][y]--;
    alive[x][y] = 0;
    ans += dis[x][y];
    tag++;
    upd(x, y);
  }
  cout << ans << endl;
  return 0;
}
