#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long xx = 0, flagg = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') {
    flagg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * flagg;
}
void pus(long long xx, long long flagg) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx >= 10) pus(xx / 10, 0);
  putchar(xx % 10 + '0');
  if (flagg == 1) putchar(' ');
  if (flagg == 2) putchar('\n');
  return;
}
int n, i, j, k, x, y, book[505][505], f[505][505][255], ans[505][505],
    bookx[25005], booky[25005], sum;
int minn(int u, int v) { return u < v ? u : v; }
int maxx(int u, int v) { return u > v ? u : v; }
int main() {
  n = read();
  for (i = 1; i <= n * n; i++) {
    x = read();
    book[(x - 1) / n + 1][(x - 1) % n + 1] = i;
    bookx[i] = (x - 1) / n + 1;
    booky[i] = (x - 1) % n + 1;
  }
  for (k = 0; k < n; k++)
    for (i = k + 1; i <= n - k; i++)
      for (j = k + 1; j <= n - k; j++)
        if (i != 1 && j != 1 && i != n && j != n) f[i][j][k] = n * n + 1;
  for (k = 0; k < n; k++)
    for (x = 1; x <= n * n; x++)
      if (bookx[x] >= k + 1 && bookx[x] <= n - k && booky[x] >= k + 1 &&
          booky[x] <= n - k) {
        i = bookx[x];
        j = booky[x];
        if (k == 0) ans[i][j] = n * n + 1;
        if (book[i - 1][j] > book[i][j] && k != 0)
          f[i][j][k] = minn(f[i][j][k], f[i - 1][j][k - 1]);
        else
          f[i][j][k] = minn(f[i][j][k], maxx(f[i - 1][j][k], book[i - 1][j]));
        if (book[i + 1][j] > book[i][j] && k != 0)
          f[i][j][k] = minn(f[i][j][k], f[i + 1][j][k - 1]);
        else
          f[i][j][k] = minn(f[i][j][k], maxx(f[i + 1][j][k], book[i + 1][j]));
        if (book[i][j - 1] > book[i][j] && k != 0)
          f[i][j][k] = minn(f[i][j][k], f[i][j - 1][k - 1]);
        else
          f[i][j][k] = minn(f[i][j][k], maxx(f[i][j - 1][k], book[i][j - 1]));
        if (book[i][j + 1] > book[i][j] && k != 0)
          f[i][j][k] = minn(f[i][j][k], f[i][j + 1][k - 1]);
        else
          f[i][j][k] = minn(f[i][j][k], maxx(f[i][j + 1][k], book[i][j + 1]));
        if (f[i][j][k] < book[i][j]) ans[i][j] = minn(ans[i][j], k);
      }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) sum += ans[i][j];
  pus(sum, 2);
  return 0;
}
