#include <bits/stdc++.h>
using namespace std;

const int MAXN = 500 + 3;
int H[MAXN][MAXN];
bool occ[MAXN][MAXN];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int x, int y) {
  int z = H[x][y] + occ[x][y];
  for (int t = 0; t < 4; t++) {
    int x1 = x + dx[t], y1 = y + dy[t];
    if (H[x1][y1] > z) {
      H[x1][y1] = z;
      dfs(x1, y1);
    }
  }
}

int main() {
  int N; cin >> N;

  for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) {
    H[i][j] = min(min(i, N+1-i), min(j, N+1-j)) - 1;
    occ[i][j] = true;
  }

  int res = 0;
  for (int it = 0; it < N*N; it++) {
    int p; cin >> p;
    int x = p % N;
    int y = (p+N-1)/N;
    if (x == 0) x += N;
    res += H[x][y];
    occ[x][y] = false;
    dfs(x, y);
  }
  cout << res << "\n";
}