#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) {
    cin >> p[i];
  }
  long long int res = 0;
  vector<int> v[n + 5];
  vector<int> vv[n + 5];
  for (int i = 0; i < n * n; i++) {
    int a = (p[i] - 1) % n;
    int b = n - 1 - a;
    int c = (p[i] - 1) / n;
    int d = n - 1 - c;
    sort(v[(p[i] - 1) % n].begin(), v[(p[i] - 1) % n].end());
    int cnt =
        upper_bound(v[(p[i] - 1) % n].begin(), v[(p[i] - 1) % n].end(), p[i]) -
        v[(p[i] - 1) % n].begin();
    int cnt2 = (int)(v[(p[i] - 1) % n].size()) - cnt;
    c -= cnt;
    d -= cnt2;
    v[(p[i] - 1) % n].push_back(p[i]);
    sort(vv[(p[i] - 1) / n].begin(), vv[(p[i] - 1) / n].end());
    cnt = upper_bound(vv[(p[i] - 1) / n].begin(), vv[(p[i] - 1) / n].end(),
                      p[i]) -
          vv[(p[i] - 1) / n].begin();
    cnt2 = (int)(vv[(p[i] - 1) / n].size()) - cnt;
    a -= cnt;
    b -= cnt2;
    vv[(p[i] - 1) / n].push_back(p[i]);
    res += min({a, b, c, d});
  }
  cout << res << endl;
}
