#include <bits/stdc++.h>
using namespace std;
int depth[500][500];
int n;
void sub(int x, int y) {
  if (depth[x][y] < 0) return;
  int r[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  for (int i = 0; i < 4; i++) {
    if (x + r[i][0] < 0 || x + r[i][0] >= n || y + r[i][1] < 0 ||
        y + r[i][1] >= n)
      continue;
    if (depth[x][y] < depth[x + r[i][0]][y + r[i][1]])
      sub(x + r[i][0], y + r[i][1]);
  }
  depth[x][y]--;
}
int main() {
  int p[250001] = {};
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> p[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      depth[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int x = (p[i] - 1) / n;
    int y = (p[i] - 1) % n;
    ans += depth[x][y];
    sub(x, y);
  }
  cout << ans << endl;
  return 0;
}
