#include <bits/stdc++.h>
using namespace std;
bool Ispossible(bool occupied[505][505], int r, int c, int n, bool hlp[][505]) {
  if (r < 0 || c < 0 || r >= n || c >= n) return false;
  if (c == 0 || c == n - 1) return true;
  bool ans1, ans2, ans3, ans4;
  if (r + 1 < n && !hlp[r + 1][c] && occupied[r + 1][c] == false) {
    hlp[r][c] = true;
    ans1 = Ispossible(occupied, r + 1, c, n, hlp);
    if (ans1) return ans1;
  }
  if (c + 1 < n && !hlp[r][c + 1] && occupied[r][c + 1] == false) {
    hlp[r][c] = true;
    ans3 = Ispossible(occupied, r, c + 1, n, hlp);
    if (ans3) return ans3;
  }
  if (r - 1 >= 0 && !hlp[r - 1][c] && occupied[r - 1][c] == false) {
    hlp[r][c] = true;
    ans2 = Ispossible(occupied, r - 1, c, n, hlp);
    if (ans2) return ans2;
  }
  if (c - 1 >= 0 && !hlp[r][c - 1] && occupied[r][c - 1] == false) {
    hlp[r][c] = true;
    ans4 = Ispossible(occupied, r, c - 1, n, hlp);
    if (ans4) return ans4;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < n * n; i++) {
    int d;
    cin >> d;
    v.push_back(d);
  }
  bool occupied[505][505];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) occupied[i][j] = true;
  }
  int ans = 0;
  for (auto i : v) {
    int r = (i - 1) / n;
    int c;
    if (i % n == 0)
      c = n - 1;
    else
      c = i % n - 1;
    if (c == 0 || c == n - 1) {
      occupied[r][c] = false;
    } else {
      bool hlp[505][505];
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) hlp[i][j] = false;
      }
      bool x = Ispossible(occupied, r, c, n, hlp);
      if (!x) ans++;
      occupied[r][c] = false;
    }
  }
  cout << ans;
  return 0;
}
