#include <bits/stdc++.h>
int tmp[500][500];
void change(int y, int x, int nn) {
  if (y - 1 >= 0) {
    if (tmp[y - 1][x] - tmp[y][x] >= 2) {
      tmp[y - 1][x] = tmp[y][x] + 1;
      change(y - 1, x, nn);
    }
  }
  if (y + 1 <= nn - 1) {
    if (tmp[y + 1][x] - tmp[y][x] >= 2) {
      tmp[y + 1][x] = tmp[y][x] + 1;
      change(y + 1, x, nn);
    }
  }
  if (x - 1 >= 0) {
    if (tmp[y][x - 1] - tmp[y][x] >= 2) {
      tmp[y][x - 1] = tmp[y][x] + 1;
      change(y, x - 1, nn);
    }
  }
  if (x + 1 <= nn - 1) {
    if (tmp[y][x + 1] - tmp[y][x] >= 2) {
      tmp[y][x + 1] = tmp[y][x] + 1;
      change(y, x + 1, nn);
    }
  }
}
int main(void) {
  int n, ans = 0, a, b;
  scanf("%d", &n);
  int p[n * n];
  for (int i = 0; i < n * n; i++) scanf("%d", &p[i]);
  for (int i = 0; i <= (n - 1) / 2; i++) {
    for (int j = i; j < n - i; j++) {
      tmp[i][j] = i;
      tmp[n - 1 - i][j] = i;
      tmp[j][i] = i;
      tmp[j][n - 1 - i] = i;
    }
  }
  for (int i = 0; i < n * n; i++) {
    a = (p[i] - 1) / n;
    b = (p[i] - 1) % n;
    ans += tmp[a][b];
    tmp[a][b]--;
    change(a, b, n);
  }
  printf("%d", ans);
}
