#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  fast();
  int n;
  cin >> n;
  vector<vector<bool> > out(n + 1, vector<bool>(n + 1, false));
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int which;
      cin >> which;
      int c = which % n;
      int r = which / n + 1;
      if (c == 0) {
        c = n;
        --r;
      }
      queue<pair<pair<int, int>, int> > que;
      vector<vector<bool> > vis(n + 1, vector<bool>(n + 1, false));
      vis[r][c] = true;
      que.push(make_pair(make_pair(r, c), 0));
      int best = 1e9;
      while (!que.empty()) {
        int curR = que.front().first.first;
        int curC = que.front().first.second;
        int hate = que.front().second;
        que.pop();
        if (curR == 1 || curC == 1 || curR == n || curC == n) {
          best = min(hate, best);
          continue;
        }
        if (!vis[curR + 1][curC]) {
          vis[curR + 1][curC] = true;
          que.push(make_pair(make_pair(curR + 1, curC),
                             hate + !out[curR + 1][curC]));
        }
        if (!vis[curR - 1][curC]) {
          vis[curR - 1][curC] = true;
          que.push(make_pair(make_pair(curR - 1, curC),
                             hate + !out[curR - 1][curC]));
        }
        if (!vis[curR][curC + 1]) {
          vis[curR][curC + 1] = true;
          que.push(make_pair(make_pair(curR, curC + 1),
                             hate + !out[curR][curC + 1]));
        }
        if (!vis[curR][curC - 1]) {
          vis[curR][curC - 1] = true;
          que.push(make_pair(make_pair(curR, curC - 1),
                             hate + !out[curR][curC - 1]));
        }
      }
      ans += best;
      out[r][c] = true;
    }
  }
  cout << ans << endl;
}
