#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-') f = -1;
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int n, a[505][505], ans, f[505][505];
int dx[] = {1, 0, 0, -1};
int dy[] = {0, 1, -1, 0};
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      f[i][j] = min(i - 1, min(n - i, min(j - 1, n - j))) + 1;
      a[i][j] = 1;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    int num = read();
    int x = (num - 1) / n + 1, y = (num - 1) % n + 1;
    queue<pair<int, int> > q;
    a[x][y] = 0, f[x][y]--, q.push({x, y});
    ans += f[x][y];
    while (!q.empty()) {
      int ux = q.front().first, uy = q.front().second;
      q.pop();
      for (int k = 0; k < 4; k++) {
        int xx = ux + dx[k];
        int yy = uy + dy[k];
        if (xx < 1 || yy < 1 || xx > n || yy > n) continue;
        if (f[ux][uy] + a[xx][yy] < f[xx][yy]) {
          f[xx][yy] = f[ux][uy] + a[xx][yy];
          q.push({xx, yy});
        }
      }
    }
  }
  cout << ans;
}
