#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int n, hate = 0, ra, ca, hatexp, hatexn, hateyp, hateyn;
  cin >> n;
  int arr[n][n], order[n * n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      arr[i][j] = 1;
    }
  }
  for (int i = 0; i < n * n; i++) {
    cin >> order[i];
  }
  for (int i = 0; i < n * n; i++) {
    ra = floor((double)(order[i] - 1) / n);
    ca = (order[i] - 1) % n;
    if (ra != 0 && ra != n - 1 && ca != 0 && ca != n - 1) {
      hatexp = hatexn = hateyp = hateyn = 0;
      for (int j = ra + 1; j < n; j++) {
        if (arr[j][ca]) {
          hatexp++;
        }
      }
      for (int j = ra - 1; j > -1; j--) {
        if (arr[j][ca]) {
          hatexn++;
        }
      }
      for (int j = ca + 1; j < n; j++) {
        if (arr[ra][j]) {
          hateyp++;
        }
      }
      for (int j = ca - 1; j > -1; j--) {
        if (arr[ra][j]) {
          hateyn++;
        }
      }
      hate += min(hatexp, min(hatexn, min(hateyp, hateyn)));
      arr[ra][ca] = 0;
    } else {
      arr[ra][ca] = 0;
    }
  }
  cout << hate << "\n";
  return 0;
}
