#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
struct dsu {
  long long n;
  vector<long long> p, dist;
  dsu(long long n) : n(n), p(n), dist(n) { iota(begin(p), end(p), 0); }
  long long find(long long x) {
    if (x == p[x]) return x;
    return p[x] = find(p[x]);
  }
  bool unite(long long x, long long y) {
    x = find(x), y = find(y);
    if (x == y) return false;
    if (rand() & 1) swap(x, y);
    p[y] = x;
    dist[x] = min(dist[x], dist[y]);
    return true;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  dsu D(n * n);
  for (long long i = (0); i < (n); i++)
    for (long long j = (0); j < (n); j++)
      D.dist[i * n + j] = min(min(i, j), min(n - i - 1, n - j - 1));
  vector<vector<long long> > mark(n, vector<long long>(n));
  long long res = 0;
  for (long long k = (0); k < (n * n); k++) {
    long long x;
    cin >> x;
    x--;
    long long i = x / n, j = x % n;
    mark[i][j] = 1;
    for (long long di = (-1); di < (2); di++)
      for (long long dj = (-1); dj < (2); dj++)
        if (abs(di) + abs(dj) == 1) {
          long long ni = i + di, nj = j + dj;
          if (ni < 0 || ni >= n || nj < 0 || nj >= n) continue;
          if (mark[ni][nj]) {
            D.unite(i * n + j, ni * n + nj);
          }
        }
    res += D.dist[D.find(i * n + j)];
  }
  cout << res << endl;
}
