#include <bits/stdc++.h>
using namespace std;
const int INF = 5000000;
const int NN_MAX = 500 * 500 + 5;
const int NIL = -1;
bool emp[NN_MAX];
int thro[NN_MAX];
int main() {
  int n;
  cin >> n;
  vector<int> P;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    P.push_back(p);
  }
  for (int i = 1; i <= n * n; i++) emp[i] = false;
  int ans = 0;
  for (int i = 0; i < P.size(); i++) {
    int p = P[i];
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        Q;
    memset(thro, INF, sizeof(thro));
    Q.push(make_pair(0, p));
    while (true) {
      int k = Q.top().second;
      int l = Q.top().first;
      Q.pop();
      if (thro[k] < l) continue;
      int h = (k - 1) / n;
      int w = k - h * n;
      if (h == 0 || h == n - 1 || w == 1 || w == n) {
        ans += l;
        break;
      }
      for (int d = -1; d <= 1; d += 2) {
        int ne = (h + d) * n + w;
        int nel = l;
        if (!emp[ne]) nel++;
        if (nel < thro[ne]) {
          Q.push(make_pair(nel, ne));
          thro[ne] = nel;
        }
      }
      for (int d = -1; d <= 1; d += 2) {
        int ne = h * n + (w + d);
        int nel = l;
        if (!emp[ne]) nel++;
        if (nel < thro[ne]) {
          Q.push(make_pair(nel, ne));
          thro[ne] = nel;
        }
      }
    }
    emp[p] = true;
  }
  cout << ans << endl;
  return 0;
}
