#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> order(n * n), ox(n * n), oy(n * n);
  for (int i = 0; i < (int)(n * n); i++) {
    cin >> order[i];
  }
  vector<bool> seat(n * n, true);
  int ans = 0;
  int dx[4] = {1, n, -1, -n};
  for (int i = 0; i < (int)(n * n); i++) {
    int a = 0;
    stack<int> s;
    int now = order[i];
    s.push(now);
    vector<bool> been(n * n, false);
    while (!(0 < now <= n || now % n == 1 || n * n - n < now <= n * n ||
             now % n == 0)) {
      now = s.top();
      s.pop();
      for (int p = 0; p < (int)(4); p++) {
        if (seat[now + dx[p]] == false) {
          s.push(now + dx[p]);
        }
      }
      if (s.empty()) {
        a++;
        for (int p = 0; p < (int)(4); p++) {
          s.push(now + dx[p]);
        }
      }
    }
  }
  return 0;
}
