#include <bits/stdc++.h>
using namespace std;
int n;
int seki[501][501];
bool visit[501][501];
bool solved[501][501];
void taiseki(int nowi, int nowj, int nowseki, bool start) {
  if (nowi <= 0 || nowi >= n - 1 || nowj <= 0 || nowj >= n - 1) {
    if (start == false) return;
  }
  if (seki[nowi][nowj] > nowseki ||
      (solved[nowi][nowj] == true && seki[nowi][nowj] == nowseki)) {
    if (visit[nowi][nowj] == false) {
      seki[nowi][nowj] = nowseki;
      visit[nowi][nowj] = true;
      if (solved[nowi][nowj] == true) nowseki--;
      taiseki(nowi - 1, nowj, nowseki + 1, false);
      taiseki(nowi + 1, nowj, nowseki + 1, false);
      taiseki(nowi, nowj - 1, nowseki + 1, false);
      taiseki(nowi, nowj + 1, nowseki + 1, false);
    }
  }
  return;
}
int main() {
  int p, pi, pj;
  int i, j, k;
  long ans = 0;
  cin >> n;
  int disti, distj;
  for (i = 0; i < n; i++) {
    disti = min(i, n - i - 1);
    for (j = 0; j < n; j++) {
      distj = min(j, n - j - 1);
      solved[i][j] = false;
      seki[i][j] = min(disti, distj);
      visit[i][j] = true;
    }
  }
  for (k = 0; k < n * n; k++) {
    cin >> p;
    pi = (p - 1) / n;
    pj = (p - 1) % n;
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        visit[i][j] = false;
      }
    }
    ans += seki[pi][pj];
    taiseki(pi, pj, seki[pi][pj] - 1, true);
    solved[pi][pj] = true;
  }
  cout << ans << endl;
  return 0;
}
