#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
bool isedge(int cur, int n) {
  if ((cur - 1) % n == 0) return true;
  if (cur % n == 0) return true;
  if (1 <= cur && cur <= n) return true;
  if (pow(n, 2) - n + 1 <= cur && cur <= pow(n, 2)) return true;
  return false;
}
int main(void) {
  int n;
  cin >> n;
  vector<int> p(pow(n, 2));
  for (int i = 0; i < pow(n, 2); i++) cin >> p[i];
  queue<pair<int, int>> que;
  vector<bool> ext(pow(n, 2), true);
  ll ans = 0;
  for (int i = 0; i < pow(n, 2); i++) {
    que.push(make_pair(p[i], 0));
    int tmp = 500;
    vector<bool> done(pow(n, 2) + 1, false);
    while (!que.empty()) {
      int cur = que.front().first;
      int uhp = que.front().second;
      que.pop();
      if (ext[cur] && cur != p[i]) uhp++;
      if (isedge(cur, n)) {
        tmp = min(tmp, uhp);
      } else if (done[cur] == false) {
        que.push(make_pair(cur - n, uhp));
        que.push(make_pair(cur - 1, uhp));
        que.push(make_pair(cur + 1, uhp));
        que.push(make_pair(cur + n, uhp));
      }
      done[cur] = true;
    }
    ext[p[i]] = false;
    ans += tmp;
  }
  cout << ans << endl;
}
