#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int x;
  int up, down, left, right, ans = 0, tmp;
  int row, col;
  int arr[250000];
  scanf("%d", &n);
  for (int i = 0; i < n * n; i++) {
    arr[i] = 1;
  }
  for (int i = 0; i < n * n; i++) {
    scanf("%d", &x);
    x--;
    arr[x] = 0;
    col = x % n;
    row = x / n;
    up = 0;
    down = 0;
    left = 0;
    right = 0;
    for (int j = (x / n) * n; j < x; j++) {
      if (arr[j] == 1) left++;
    }
    for (int j = x + 1; j < (x / n + 1) * n; j++) {
      if (arr[j] == 1) right++;
    }
    for (int j = x % n; j < x; j += n) {
      if (arr[j] == 1) up++;
    }
    for (int j = x + n; j < n * n; j += n) {
      if (arr[j] == 1) down++;
    }
    tmp = min(left, right);
    tmp = min(tmp, up);
    tmp = min(tmp, down);
    ans += tmp;
  }
  printf("%d\n", ans);
  return 0;
}
