#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pii = pair<int, pi>;
int main() {
  int n;
  cin >> n;
  vector<int> vd = {-n, +1, +n, -1};
  vector<bool> seki(n * n, true);
  ll ans = 0;
  int p;
  vector<int> dp(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p;
    --p;
    deque<pi> deq;
    dp.assign(n * n, n * n);
    dp[p] = 0;
    deq.push_front(pi(0, p));
    int tans;
    while (!deq.empty()) {
      auto t = deq.front();
      deq.pop_front();
      for (int i = 0; i < 4; ++i) {
        int np = t.second + vd[i];
        if (np < 0 || n * n <= np) {
          tans = t.first;
          if (!deq.empty()) deq.clear();
          break;
        }
        if (t.second % n == n - 1 && i == 1) {
          tans = t.first;
          if (!deq.empty()) deq.clear();
          break;
        }
        if (t.second % n == 0 && i == 3) {
          tans = t.first;
          if (!deq.empty()) deq.clear();
          break;
        }
        int nc = t.first;
        if (seki[np]) ++nc;
        if (dp[np] <= nc) continue;
        dp[np] = nc;
        if (seki[np]) {
          deq.push_back(pi(nc, np));
        } else
          deq.push_front(pi(nc, np));
      }
    }
    ans += tans;
    seki[p] = false;
  }
  cout << ans << endl;
  return 0;
}
