#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y, w;
  node(int nx = 0, int ny = 0) {
    x = nx;
    y = ny;
  }
} x[300005], v;
int n, y, lim, nx, ny, mov[4][2] = {0, 1, 0, -1, 1, 0, -1, 0},
                       dp[505][505][505], p, q[505][505], cnt = 0, ans = 0;
vector<node> dpr[300005];
void upd(int x, int y, int w) {
  if (max(w, q[x][y]) < max(dp[x][y][p], q[x][y]))
    dpr[max(w, q[x][y])].push_back(node(x, y));
  dp[x][y][p] = w;
}
int main() {
  scanf("%d", &n);
  lim = n / 2 - 1;
  for (int i = 1; i <= n * n; i++) {
    scanf("%d", &y);
    if (y % n == 0)
      q[y / n][n] = i;
    else
      q[y / n + 1][y % n] = i;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      for (int k = 0; k <= lim; k++) dp[i][j][k] = n * n + 2;
  for (int i = 1; i <= n; i++)
    for (int k = 0; k <= lim; k++)
      dp[1][i][k] = dp[n][i][k] = dp[i][1][k] = dp[i][n][k] = 0;
  for (p = 0; p <= lim; p++) {
    cnt = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dpr[max(dp[i][j][p], q[i][j])].push_back(node(i, j));
    for (int i = 0; i < n * n; i++) {
      for (int j = 0; j < dpr[i].size(); j++) {
        v = dpr[i][j];
        if (i != max(dp[v.x][v.y][p], q[v.x][v.y])) continue;
        for (int k = 0; k < 4; k++) {
          nx = v.x + mov[k][0];
          ny = v.y + mov[k][1];
          if (min(nx, ny) < 1 || max(nx, ny) > n) continue;
          if (dp[nx][ny][p] > i) {
            upd(nx, ny, i);
          }
        }
      }
      dpr[i].clear();
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        for (int k = 0; k < 4; k++) {
          nx = i + mov[k][0];
          ny = j + mov[k][1];
          if (min(nx, ny) < 1 || max(nx, ny) > n) continue;
          dp[nx][ny][p + 1] = min(dp[nx][ny][p + 1], dp[i][j][p]);
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 0; k <= lim; k++) {
        if (dp[i][j][k] <= q[i][j]) {
          ans += k;
          break;
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
