#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
using namespace std;
unsigned char D[252004];
bool E[252004];
unsigned int Q[200000];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int n = N + 2;
  for (int i = 1; i <= (N); i++) {
    int ij = n * i + 1;
    unsigned char kari = min(i - 1, N - i) + 2;
    unsigned char kari2 = 2;
    for (int j = 1; j <= (N / 2); j++) {
      D[ij++] = min(kari, kari2++);
    }
    if (N % 2 == 0) kari2--;
    for (int j = N / 2 + 1; j <= N; j++) {
      D[ij++] = min(kari, kari2--);
    }
  }
  int kotae = 0;
  for (int k = 0; k < (N * N); k++) {
    int ij;
    cin >> ij;
    ij += (ij - 1) / N * 2 + N + 2;
    kotae += D[ij]--;
    E[ij] = 1;
    int p = 0, q = 0;
    Q[p++] = ij;
    while (p > q) {
      int ij = Q[q++];
      int d = D[ij] + 1;
      if (D[ij - n] > d - E[ij - n]) {
        D[ij - n] = d - E[ij - n];
        Q[p++] = ij - n;
      }
      if (D[ij + n] > d - E[ij + n]) {
        D[ij + n] = d - E[ij + n];
        Q[p++] = ij + n;
      }
      if (D[ij - 1] > d - E[ij - 1]) {
        D[ij - 1] = d - E[ij - 1];
        Q[p++] = ij - 1;
      }
      if (D[ij + 1] > d - E[ij + 1]) {
        D[ij + 1] = d - E[ij + 1];
        Q[p++] = ij + 1;
      }
    }
  }
  cout << (kotae - 2 * N * N) << "\n";
  return 0;
}
