#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int n, x, y, l, r, dis[510][510], dx[5], dy[5];
bool f[510][510];
struct da {
  int x, y;
} q[maxn];
long long ans;
inline bool ok(int x, int y) {
  if (x >= 1 && x <= n && y >= 1 && y <= n) return 1;
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      dis[i][j] = i - 1;
      dis[i][j] = min(dis[i][j], j - 1);
      dis[i][j] = min(dis[i][j], n - i);
      dis[i][j] = min(dis[i][j], n - j);
      f[i][j] = 1;
    }
  dx[0] = 1;
  dy[0] = 0;
  dx[1] = -1;
  dy[1] = 0;
  dx[2] = 0;
  dy[2] = 1;
  dx[3] = 0;
  dy[3] = -1;
  for (int i = 1; i <= n * n; i++) {
    int k;
    scanf("%d", &k);
    x = k / n + 1;
    y = k % n;
    ans += dis[x][y];
    f[x][y] = 0;
    l = r = 1;
    q[1] = {x, y};
    while (l <= r) {
      da now = q[l++];
      for (int j = 0; j < 4; j++)
        if (ok(now.x + dx[j], now.y + dy[j]) &&
            dis[now.x + dx[j]][now.y + dy[j]] >
                dis[now.x][now.y] + f[now.x][now.y]) {
          dis[now.x + dx[j]][now.y + dy[j]] =
              dis[now.x][now.y] + f[now.x][now.y];
          q[++r] = {now.x + dx[j], now.y + dy[j]};
        }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
