#include <bits/stdc++.h>
using namespace std;
const int xx[] = {1, 0, -1, 0}, yy[] = {0, 1, 0, -1};
int n;
int arrX[250002], arrY[250002];
int DP[502][502];
int ans;
bool chk[502][502];
bool chk2[502][502];
queue<pair<int, int> > que;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++) {
    int tmp;
    scanf("%d", &tmp);
    arrX[i] = (tmp + n - 1) / n;
    arrY[i] = (tmp - 1) % n + 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      chk[i][j] = 1;
      DP[i][j] = min({i, j, n - i + 1, n - j + 1});
    }
  }
  for (int tc = 1; tc <= n * n; tc++) {
    int tx = arrX[tc], ty = arrY[tc];
    ans += DP[tx][ty] - 1;
    chk[tx][ty] = 0;
    chk2[tx][ty] = 1;
    que.push(make_pair(tx, ty));
    while (!que.empty()) {
      pair<int, int> tmp = que.front();
      que.pop();
      tx = tmp.first, ty = tmp.second;
      DP[tx][ty]--;
      chk2[tx][ty] = 0;
      for (int d = 0; d < 4; d++) {
        int ttx = tx + xx[d], tty = ty + yy[d];
        if (chk2[tx][ty] || ttx < 1 || ttx > n || tty < 1 || tty > n ||
            DP[ttx][tty] - chk[ttx][tty] != DP[tx][ty] + 1)
          continue;
        que.push(make_pair(ttx, tty));
        chk2[tx][ty] = 1;
      }
    }
  }
  printf("%d", ans);
}
