#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];
  vector<set<int> > h(n), w(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      h[i].insert(j);
      w[i].insert(j);
    }
  }
  long long ans = 0;
  for (int x : p) {
    x--;
    int i = x / n;
    int j = x % n;
    long long tmp = n;
    auto hl = h[i].lower_bound(j);
    auto hb = h[i].begin();
    auto hu = h[i].upper_bound(j);
    auto he = h[i].end();
    auto wl = w[j].lower_bound(i);
    auto wb = w[j].begin();
    auto wu = w[j].upper_bound(i);
    auto we = w[j].end();
    long long d0 = distance(hb, hl);
    long long d1 = distance(hu, he);
    long long d2 = distance(wb, wl);
    long long d3 = distance(wu, we);
    tmp = min(tmp, d0);
    tmp = min(tmp, d1);
    tmp = min(tmp, d2);
    tmp = min(tmp, d3);
    ans += tmp;
    h[i].erase(j);
    w[j].erase(i);
  }
  cout << ans << "\n";
  return 0;
}
