#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, count;
  cin >> n;
  vector<long long> vec(n * n);
  for (int i = 0; i < n * n; i++) {
    cin >> vec.at(i);
  }
  vector<long long> a(n * n + n);
  for (int i = 0; i < n * n; i++) {
    if ((i + 1) % n == 0 || (i + 1) % n == 1 || (0 <= i && i < n) ||
        (n * (n - 1) < i + 1 && i + 1 <= n * n)) {
      a.at(i) = 0;
    } else {
      a.at(i) = 1;
    }
  }
  for (int i = 0; i < n * n; i++) {
    if (a.at(vec.at(i) - 1) == 1) {
      count++;
      a.at(i) = 2;
    } else if (a.at(vec.at(i) - 1) == 0) {
      if (i % n >= 1) {
        a.at(i - 1) = 0;
      }
      if (i % n <= 4) {
        a.at(i + 1) = 0;
      }
      if (i / n >= 1) {
        a.at(i - 6) = 0;
      }
      if (i / n < 5) {
        a.at(i + 6) = 0;
      }
    }
    for (int j = n + 1; j < n * n; j++) {
      if (a.at(j) == 2) {
        if (a.at(j + 6) == 0 || a.at(j - 6) == 0 ||
            (a.at(j - 1) == 0 && j % n > 0) ||
            (a.at(j + 1) == 0 && j % n < 5)) {
          a.at(j - 6) = 0;
          a.at(j + 6);
          if (j % n > 0) {
            a.at(j - 1) = 0;
          }
          if (j % n < 5) {
            a.at(j + 1) = 0;
          }
        }
      }
    }
  }
  cout << count << endl;
}
