#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
using namespace std;
int D[502][502];
bool E[502][502];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  for (int i = 1; i <= (N); i++)
    for (int j = 1; j <= (N); j++) {
      D[i][j] = min(i - 1, j - 1);
      int kari = min(N - i, N - j);
      D[i][j] = min(D[i][j], kari);
      E[i][j] = 1;
    }
  long long kotae = 0;
  for (int k = 0; k < (N * N); k++) {
    int p;
    cin >> p;
    p--;
    int i = p / N + 1;
    int j = p % N + 1;
    kotae += D[i][j];
    D[i][j]--;
    E[i][j] = 0;
    queue<pair<int, int>> Q;
    Q.push(make_pair(i, j));
    while (Q.size()) {
      auto p = Q.front();
      Q.pop();
      i = p.first;
      j = p.second;
      int d = D[i][j];
      if (D[i - 1][j] > d + E[i - 1][j]) {
        D[i - 1][j] = d + E[i - 1][j];
        Q.push(make_pair(i - 1, j));
      }
      if (D[i + 1][j] > d + E[i + 1][j]) {
        D[i + 1][j] = d + E[i + 1][j];
        Q.push(make_pair(i + 1, j));
      }
      if (D[i][j - 1] > d + E[i][j - 1]) {
        D[i][j - 1] = d + E[i][j - 1];
        Q.push(make_pair(i, j - 1));
      }
      if (D[i][j + 1] > d + E[i][j + 1]) {
        D[i][j + 1] = d + E[i][j + 1];
        Q.push(make_pair(i, j + 1));
      }
    }
  }
  cout << (kotae) << "\n";
  return 0;
}
