#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int dis[N][N][N / 2], P[N][N], n;
int odrad[N][N][N / 2];
queue<pair<pair<int, int>, int> > Q;
const int px[4] = {1, -1, 0, 0};
const int py[4] = {0, 0, 1, -1};
inline int get_dis(int x, int y) { return min(min(x, y), n - max(x, y) - 1); }
int main() {
  memset(dis, INF, sizeof(dis));
  memset(odrad, -1, sizeof(odrad));
  scanf("%d", &n);
  for (int i = 0; i < n * n; i++) {
    int x;
    scanf("%d", &x);
    x--;
    P[x % n][x / n] = i;
  }
  for (int i = 0; i < n; i++) {
    Q.push({{0, i}, 0});
    dis[0][i][0] = 0;
    Q.push({{n - 1, i}, 0});
    dis[n - 1][i][0] = 0;
    Q.push({{i, 0}, 0});
    dis[i][0][0] = 0;
    Q.push({{i, n - 1}, 0});
    dis[i][n - 1][0] = 0;
  }
  for (; !Q.empty(); Q.pop()) {
    int cx = Q.front().first.first;
    int cy = Q.front().first.second;
    int ck = Q.front().second;
    if (odrad[cx][cy][ck] == dis[cx][cy][ck]) continue;
    if (ck && dis[cx][cy][ck - 1] <= dis[cx][cy][ck]) continue;
    odrad[cx][cy][ck] = dis[cx][cy][ck];
    for (int m = 0; m < 4; m++) {
      int mx = cx + px[m], my = cy + py[m];
      if (mx < 0 || mx >= n || my < 0 || my >= n) continue;
      if (max(dis[cx][cy][ck], P[cx][cy]) < dis[mx][my][ck]) {
        dis[mx][my][ck] = max(dis[cx][cy][ck], P[cx][cy]);
        Q.push({{mx, my}, ck});
      }
      if (ck < get_dis(mx, my) && dis[cx][cy][ck] < dis[mx][my][ck + 1] &&
          dis[cx][cy][ck] < dis[mx][my][ck]) {
        dis[mx][my][ck + 1] = dis[cx][cy][ck];
        Q.push({{mx, my}, ck + 1});
      }
    }
  }
  int odg = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int k = 0;
      for (; dis[i][j][k] > P[i][j]; k++)
        ;
      odg += k;
    }
  }
  printf("%d\n", odg);
  return 0;
}
