#include <bits/stdc++.h>
using namespace std;
const int Q = 505;
int ans[Q][Q];
int tx[] = {0, 0, -1, 1}, ty[] = {-1, 1, 0, 0};
int qx[Q * Q], qy[Q * Q];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      ans[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
  int qaq = n * n;
  int tot = 0;
  while (qaq--) {
    int x, y, p;
    scanf("%d", &p);
    x = (p - 1) / n + 1, y = p - (x - 1) * n;
    --ans[x][y], tot += ans[x][y];
    int hd = 1, tl = 0;
    qx[++tl] = x, qy[tl] = y;
    while (hd <= tl) {
      int xx = qx[hd], yy = qy[hd++];
      for (int t = 0; t < 4; t++) {
        int x0 = xx + tx[t], y0 = yy + ty[t];
        if (x0 >= 1 && x0 <= n && y0 >= 1 && y0 <= n &&
            ans[xx][yy] + 1 < ans[x0][y0])
          ans[x0][y0] = ans[xx][yy] + 1, qx[++tl] = x0, qy[tl] = y0;
      }
    }
  }
  printf("%d\n", tot);
  return 0;
}
