#include <bits/stdc++.h>
using namespace std;
long long mod = (1e9) + 7;
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long *ar = new long long[n * n];
  int count = 0;
  unordered_set<int> set;
  for (long long i = 0; i < (n * n); i++) {
    cin >> ar[i];
    int rs = (n * ((ar[i] - 1) / n)) + 1;
    int re = rs + n - 1;
    int cs = ar[i] % n;
    if (cs == 0) cs = n;
    int ce = cs + ((n - 1) * n);
    int a = 0, b = 0, c = 0, d = 0;
    for (int j = ar[i] + 1; j <= re; j++) {
      if (set.find(j) == set.end()) a++;
    }
    for (int j = ar[i] - 1; j >= rs; j--) {
      if (set.find(j) == set.end()) b++;
    }
    for (int j = ar[i] - n; j >= cs; j -= n) {
      if (set.find(j) == set.end()) c++;
    }
    for (int j = ar[i] + n; j <= ce; j += n) {
      if (set.find(j) == set.end()) d++;
    }
    count += min(a, min(b, min(c, d)));
    set.insert(ar[i]);
  }
  cout << count << endl;
}
