#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return l + rng() % (r - l + 1); }
const int N = 510;
int n, tot;
int f[N][N], a[N][N];
int p[N * N];
int D[] = {-1, 0, 1, 0};
int C[] = {0, 1, 0, -1};
int main() {
  n = ({
    int x = 0;
    int c = getchar(), n = 0;
    for (; !isdigit(c); c = getchar()) n = (c == '-');
    for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
    n ? -x : x;
  });
  for (int i = 1, _c = n * n; i <= _c; ++i)
    p[i] = ({
      int x = 0;
      int c = getchar(), n = 0;
      for (; !isdigit(c); c = getchar()) n = (c == '-');
      for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
      n ? -x : x;
    });
  for (int i = 1, _c = n; i <= _c; ++i)
    for (int j = 1, _c = n; j <= _c; ++j) {
      f[i][j] = min({i, n - i + 1, j, n - j + 1}) - 1;
      a[i][j] = 1;
    }
  for (int w = 1, _c = n * n; w <= _c; ++w) {
    int i = (p[w] - 1) / n + 1, j = p[w] - (i - 1) * n;
    tot += f[i][j];
    queue<pair<int, int> > q;
    q.push({i, j});
    a[i][j] = 0;
    while (q.size()) {
      tie(i, j) = q.front();
      q.pop();
      int val = f[i][j] + a[i][j];
      for (int t = 0, _c = 3; t <= _c; ++t) {
        int x = i + D[t], y = j + C[t];
        if (1 > x || x > n || 1 > y || y > n || f[x][y] <= val) continue;
        f[x][y] = val;
        q.push({x, y});
      }
    }
  }
  cout << tot;
}
