#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  vector<vector<bool>> isThere(n, vector<bool>(n, true));
  int ans = 0;
  for (int w = 0; w < n * n; w++) {
    int p;
    cin >> p;
    p--;
    int s = p / n;
    int t = p - s * n;
    P start(s, t);
    queue<P> q;
    q.push(start);
    isThere[s][t] = false;
    vector<vector<bool>> sudenikita(n, vector<bool>(n));
    int hate = 0;
    int ds[] = {0, 1, 0, -1};
    int dt[] = {1, 0, -1, 0};
    bool flag = false;
    while (!q.empty()) {
      P now = q.front();
      q.pop();
      sudenikita[now.first][now.second] = true;
      if (isThere[now.first][now.second]) hate++;
      for (int i = 0; i < 4; i++) {
        int nexts = s + ds[i];
        int nextt = t + dt[i];
        if (nexts < 0 || nexts >= n || nextt < 0 || nextt >= n) {
          flag = true;
          break;
        }
        if (sudenikita[nexts][nextt]) continue;
        P next(nexts, nextt);
        q.push(next);
      }
      if (flag) break;
    }
    ans += hate;
  }
  cout << ans << endl;
  return 0;
}
