#include <bits/stdc++.h>
using namespace std;
struct ufind {
  vector<int> uf;
  ufind(int q) {
    uf = vector<int>(q);
    for (int i = 0; i < q; i++) uf.at(i) = i;
  }
  ufind(vector<int> q) { uf = q; }
  void stick(int a, int b) {
    if (a == b) return;
    a = fpe(a);
    b = fpe(b);
    int minx = (a < b) ? a : b;
    uf.at(a) = minx;
    uf.at(b) = minx;
  }
  int fpe(int a) { return (uf.at(a) == a) ? a : uf.at(a) = fpe(uf.at(a)); }
  bool find(int a, int b) {
    if (a == b) return true;
    return fpe(a) == fpe(b);
  }
};
int main() {
  int N;
  cin >> N;
  vector<vector<int>> seat(N + 2, vector<int>(N + 2));
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      seat.at(i).at(j) = 1;
    }
  }
  int S = N + 2;
  ufind uf(S * S);
  int NN = N * N;
  int outseat;
  int hate = 0;
  while (NN) {
    cin >> outseat;
    int sx = (outseat - 1) % N + 1;
    int sy = ((outseat - 1) / N) + 1;
    for (int i = 0; i < S; i++) {
      for (int j = 0; j < S; j++) {
        if (i && (!seat.at(i - 1).at(j) && !seat.at(i).at(j))) {
          uf.stick((i - 1) * S + j, i * S + j);
        }
        if (j && (!seat.at(i).at(j - 1) && !seat.at(i).at(j))) {
          uf.stick(i * S + j - 1, i * S + j);
        }
      }
    }
    ufind M(uf.uf);
    auto Mseat = seat;
    Mseat.at(sy).at(sx) = 0;
    int cucu = sy * S + sx;
    while (true) {
      for (int i = 0; i < S; i++) {
        for (int j = 0; j < S; j++) {
          if (i && (!Mseat.at(i - 1).at(j) && !Mseat.at(i).at(j))) {
            M.stick((i - 1) * S + j, i * S + j);
          }
          if (j && (!Mseat.at(i).at(j - 1) && !Mseat.at(i).at(j))) {
            M.stick(i * S + j - 1, i * S + j);
          }
        }
      }
      if (!M.fpe(cucu)) break;
      for (int i = 0; i < S; i++) {
        for (int j = 0; j < S; j++) {
          int Mroot = M.uf.at(cucu);
          int current = i * S + j;
          if (i && (M.uf.at((i - 1) * S + j) == Mroot ||
                    M.uf.at(current) == Mroot)) {
            Mseat.at(i - 1).at(j) = 0;
            Mseat.at(i).at(j) = 0;
          }
          if (j &&
              (M.uf.at(current - 1) == Mroot || M.uf.at(current) == Mroot)) {
            Mseat.at(i).at(j - 1) = 0;
            Mseat.at(i).at(j) = 0;
          }
        }
      }
      hate++;
    }
    seat.at(sy).at(sx) = 0;
    NN--;
  }
  cout << hate << endl;
}
