#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) {
    cin >> p[i];
  }
  int seet[n][n];
  for (int i = 0; i < n * n; i++) {
    seet[i / n][i % n] = i + 1;
  }
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    long long buf = 0, aans = 1LL << 60;
    for (int j = 0; j < (p[i] - 1) / n; j++) {
      if (seet[j][(p[i] - 1) % n] != -1) buf++;
    }
    aans = min(aans, buf);
    buf = 0;
    for (int j = n - 1; j > (p[i] - 1) / n; j--) {
      if (seet[j][(p[i] - 1) % n] != -1) buf++;
    }
    aans = min(aans, buf);
    buf = 0;
    for (int j = 0; j < (p[i] - 1) % n; j++) {
      if (seet[(p[i] - 1) / n][j] != -1) buf++;
    }
    aans = min(aans, buf);
    buf = 0;
    for (int j = n - 1; j > (p[i] - 1) % n; j--) {
      if (seet[(p[i] - 1) / n][j] != -1) buf++;
    }
    aans = min(aans, buf);
    seet[(p[i] - 1) / n][(p[i] - 1) % n] = -1;
    ans += aans;
  }
  cout << ans << endl;
}
