#include <bits/stdc++.h>
int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }
int main() {
  int n;
  scanf("%d", &n);
  int p;
  int i, j;
  int a[n][n];
  int k = 1;
  int x[4] = {1, 0, -1, 0};
  int y[4] = {0, -1, 0, 1};
  long long ans = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      a[i][j] = k;
      k++;
    }
  }
  for (i = 0; i < n * n; i++) {
    scanf("%d", &p);
    int cx;
    if (p % n == 0) {
      cx = n - 1;
    } else {
      cx = (p % n) - 1;
    }
    int cy = p / n;
    int count[4] = {};
    a[cy][cx] = 0;
    for (j = 0; j < 4; j++) {
      int yoko = x[j];
      int tate = y[j];
      int tmpx = cx, tmpy = cy;
      while (1) {
        if (tmpx == 0 || tmpx == n - 1 || tmpy == 0 || tmpy == n - 1) {
          break;
        }
        tmpx += yoko;
        tmpy += tate;
        if (a[tmpy][tmpx] != 0) {
          count[j]++;
        }
      }
    }
    qsort(count, 4, sizeof(int), cmp);
    ans += count[0];
  }
  printf("%lld\n", ans);
  return 0;
}
