#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> adj = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<vector<long long>> seats(510, vector<long long>(510, -1));
vector<vector<long long>> soon(510, vector<long long>(510, 0));
void dfs1(long long x, long long y) {
  seats[x][y]--;
  for (long long i = 0; i < (long long)(4); i++) {
    long long nex = x + adj[i][0];
    long long ney = y + adj[i][1];
    if ((soon[nex][ney] == 1) && (seats[nex][ney] == seats[x][y] + 1)) {
      dfs1(nex, ney);
    } else if (seats[nex][ney] == seats[x][y] + 1) {
      seats[nex][ney]--;
    }
  }
}
void dfs2(long long x, long long y) {
  for (long long i = 0; i < (long long)(4); i++) {
    long long nex = x + adj[i][0];
    long long ney = y + adj[i][1];
    if (seats[nex][ney] > seats[x][y]) {
      dfs2(nex, ney);
    }
  }
  seats[x][y]--;
}
int main() {
  long long n, p;
  cin >> n;
  long long ind = 1;
  while (ind <= n + 1 - ind) {
    for (long long i = (ind); i <= (n + 1 - ind); i++)
      for (long long j = (ind); j <= (n + 1 - ind); j++) {
        seats[i][j] = ind - 1;
      }
    ind++;
  }
  long long ans = 0;
  for (long long i = 0; i < (long long)(n * n); i++) {
    cin >> p;
    long long h = (p - 1) / n + 1;
    long long w = (p - 1) % n + 1;
    ans += seats[h][w];
    soon[h][w] = 1;
    for (long long j = 0; j < (long long)(4); j++) {
      long long adh = h + adj[j][0];
      long long adw = w + adj[j][1];
      if (soon[adh][adw] == 1 && seats[adh][adw] > seats[h][w]) {
        dfs1(adh, adw);
      } else if (soon[adh][adw] == 0 && seats[adh][adw] > seats[h][w]) {
        dfs2(adh, adw);
      }
    }
  }
  cout << ans << endl;
}
