#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n, a[N][N], ans, par[N * N], cost[N * N];
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
int find(int x) {
  if (x == par[x]) return x;
  return par[x] = find(par[x]);
}
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  par[x] = y;
  cost[y] = min(cost[x], cost[y]);
}
int f(int r, int c) { return (r - 1) * n + c; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a[i][j] = 1;
      cost[f(i, j)] = min(min(i - 1, n - i), min(j - 1, n - j));
    }
  }
  for (int i = 1; i <= n * n; i++) {
    par[i] = i;
  }
  int x;
  for (int i = 1; i <= n * n; i++) {
    cin >> x;
    int r = (x - 1) / n + 1;
    int c = x - (r - 1) * n;
    int now = cost[find(x)];
    for (int j = 1; j < 4; j++) {
      if (!a[r + dr[j]][c + dc[j]]) {
        now = min(now, cost[find(f(r + dr[j], c + dc[j]))]);
        merge(x, f(r + dr[j], c + dc[j]));
      }
    }
    ans += now;
    a[r][c] = 0;
  }
  cout << ans << "\n";
}
