#include <bits/stdc++.h>
using namespace std;
int n, x, t, ans, num = 0;
int p[250005], fa[250005];
bool q[250005] = {0};
int min4(int a, int b, int c, int d) {
  if (b < a) a = b;
  if (c < a) a = c;
  if (d < a) a = d;
  return a;
}
int find(int x) {
  if (x == fa[x]) return x;
  fa[x] = find(fa[x]);
  return fa[x];
}
void merge(int x, int y) { fa[x] = y; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      int tt = n * (i - 1) + j;
      p[tt] = min4(i - 1, j - 1, n - i, n - j);
      fa[tt] = tt;
    }
  int dx[] = {-n, n, -1, 1};
  for (int k = 1; k <= n * n; k++) {
    cin >> t;
    q[t] = 1;
    for (int i = 0; i < 4; i++) {
      x = t + dx[i];
      if (t % n == 0 && dx[i] == 1) continue;
      if (t % n == 1 && dx[i] == -1) continue;
      if (x > n * n || x < 0 || !q[x]) continue;
      x = find(x);
      p[t] = min(p[t], p[x]);
      merge(x, t);
    }
    ans += p[t];
  }
  cout << ans;
  return 0;
}
