#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
using ll = long long;
int main() {
  int N;
  cin >> N;
  vector<int> P(N * N);
  vector<int> V(N * N);
  for (int i = 0; i < (int)(N * N); i++) cin >> P.at(i);
  for (int i = 0; i < (int)(N * N); i++) V.at(i) = i + 1;
  int ans = 0;
  for (int i = 0; i < (int)(N * N); i++) {
    int check = P.at(i);
    int flag_up = 1;
    int flag_down = 1;
    int flag_right = 1;
    int flag_left = 1;
    if (check > N) {
      while (check > N) {
        check -= N;
        if (V.at(check - 1) == 0) {
          flag_up = 0;
        } else {
          flag_up = 1;
          break;
        }
      }
    } else {
      flag_up = 0;
    }
    check = P.at(i);
    if (check + N <= N * N) {
      while (check + N <= N * N) {
        check += N;
        if (V.at(check - 1) == 0) {
          flag_down = 0;
        } else {
          flag_down = 1;
          break;
        }
      }
    } else {
      flag_down = 0;
    }
    check = P.at(i);
    if (check % N != 0) {
      while (check % N != 0) {
        check += 1;
        if (V.at(check - 1) == 0) {
          flag_right = 0;
        } else {
          flag_right = 1;
          break;
        }
      }
    } else {
      flag_right = 0;
    }
    check = P.at(i);
    if (check % N != 1) {
      while (check % N != 1) {
        check -= 1;
        if (V.at(check - 1) == 0) {
          flag_left = 0;
        } else {
          flag_left = 1;
          break;
        }
      }
    } else {
      flag_left = 0;
    }
    check = P.at(i);
    if (flag_up * flag_down * flag_right * flag_left != 0) {
      ans += 1;
    }
    V.at(check - 1) = 0;
  }
  cout << ans << endl;
  return 0;
}
