#include <bits/stdc++.h>
using namespace std;
struct g {
  int u;
  int d;
  int r;
  int l;
};
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) cin >> p.at(i);
  vector<vector<g>> cnt(n, vector<g>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cnt.at(i).at(j).u = i;
      cnt.at(i).at(j).d = n - i - 1;
      cnt.at(i).at(j).l = j;
      cnt.at(i).at(j).r = n - j - 1;
    }
  }
  long long ans = 0;
  for (int k = 0; k < n * n; k++) {
    int now = p.at(k);
    int x = (now - 1) % n, y = (now - 1) / n;
    long long plus =
        min(min(cnt.at(y).at(x).u, cnt.at(y).at(x).d), cnt.at(y).at(x).r);
    plus = min(plus, (long long)cnt.at(y).at(x).l);
    plus = max(plus, (long long)0);
    ans += plus;
    for (int i = 0; i < n; i++) {
      if (i < x) cnt.at(y).at(i).r--;
      if (i > x) cnt.at(y).at(i).l--;
    }
    for (int i = 0; i < n; i++) {
      if (i < y) cnt.at(i).at(x).d--;
      if (i > y) cnt.at(i).at(x).u--;
    }
  }
  cout << ans << endl;
}
