#include <bits/stdc++.h>
using namespace std;
int T, a, i, j, q, s;
long long N;
bitset<300040> sos;
int main() {
  cin >> N;
  int ans = 0;
  for (i = 0; i < N * N; i++) {
    cin >> a;
    q = N;
    sos[a] = 1;
    s = 0;
    for (j = a; j > 0; j -= N) {
      s += 1 - sos[j];
    }
    q = min(s, q);
    s = 0;
    for (j = a; j <= N * N; j += N) {
      s += 1 - sos[j];
    }
    q = min(s, q);
    s = 0;
    for (j = a; j % N; j--) {
      s += 1 - sos[j];
    }
    q = min(s, q);
    s = 0;
    for (j = a; (j - 1) % N; j++) {
      s += 1 - sos[j];
    }
    q = min(s, q);
    ans += q;
  }
  cout << ans;
  return 0;
}
