#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using graph = vector<vector<int>>;
int main() {
  int n, h, hatesum = 0, hatemin = 1000;
  cin >> n;
  vi nx = {0, 1, 0, -1};
  vi ny = {1, 0, -1, 0};
  graph be(n, vi(n, 1));
  vector<pair<int, int>> sq;
  for (int i = 0; i < pow(n, 2); i++) {
    cin >> h;
    pair<int, int> p = make_pair((h - 1) / n, (h - 1) % n);
    sq.push_back(p);
  }
  for (int i = 0; i < pow(n, 2); i++) {
    pair<int, int> np = sq[i];
    int py = np.first;
    int px = np.second;
    be[py][px] = 0;
    graph hate(n, vi(n, -1));
    hate[py][px] = 0;
    queue<pair<int, int>> que = {};
    que.push(np);
    while (!que.empty()) {
      pair<int, int> nq = que.front();
      que.pop();
      int nqy = nq.first;
      int nqx = nq.second;
      for (int j = 0; j < 4; j++) {
        int nny = ny[j];
        int nnx = nx[j];
        if (nqy + nny < 0 || nqy + nny == n || nqx + nnx < 0 ||
            nqx + nny == n) {
          continue;
        } else if (hate[nqy + nny][nqx + nnx] != -1) {
          continue;
        } else {
          hate[nqy + nny][nqx + nnx] = hate[nqy][nqx];
          pair<int, int> nnq = make_pair(nqy + nny, nqx + nnx);
          que.push(nnq);
          if (be[nqy + nny][nqx + nnx] == 1) {
            hate[nqy + nny][nqx + nnx]++;
          }
        }
      }
    }
    hatemin = 1000;
    for (int j = 0; j < n; j++) {
      hatemin = min(hatemin, min(hate[0][j], hate[n - 1][j]));
      hatemin = min(hatemin, min(hate[j][0], hate[j][n - 1]));
    }
    hatesum += hatemin;
  }
  cout << hatesum << endl;
}
