#include <bits/stdc++.h>
using namespace std;
int n;
void print_seat(int *seat) {
  for (int i = 0; i < n + 2; i++) {
    for (int j = 0; j < n + 2; j++) {
      if (seat[i * (n + 2) + j] == 0) {
        cout << ' ';
      } else if (seat[i * (n + 2) + j] == 1) {
        cout << '*';
      } else {
        cout << 'x';
      }
    }
    cout << endl;
  }
  cout << endl;
}
int leave(int row, int col, int bad_pair, int *seat) {
  if (seat[(row + 1) * (n + 2) + col] == 0 ||
      seat[(row - 1) * (n + 2) + col] == 0 ||
      seat[row * (n + 2) + col + 1] == 0 ||
      seat[row * (n + 2) + col - 1] == 0) {
    return bad_pair;
  }
  seat[row * (n + 2) + col] = 2;
  bad_pair += 1;
  int pair = n;
  if (seat[(row + 1) * (n + 2) + col] != 2) {
    int tmp = leave(row + 1, col, bad_pair, seat);
    if (tmp < pair) {
      pair = tmp;
    }
  }
  if (seat[(row - 1) * (n + 2) + col] != 2) {
    int tmp = leave(row - 1, col, bad_pair, seat);
    if (tmp < pair) {
      pair = tmp;
    }
  }
  if (seat[row * (n + 2) + col + 1] != 2) {
    int tmp = leave(row, col + 1, bad_pair, seat);
    if (tmp < pair) {
      pair = tmp;
    }
  }
  if (seat[row * (n + 2) + col - 1] != 2) {
    int tmp = leave(row, col - 1, bad_pair, seat);
    if (tmp < pair) {
      pair = tmp;
    }
  }
  seat[row * (n + 2) + col] = 1;
  return pair;
}
int main() {
  cin >> n;
  int order[n * n];
  for (int i = 0; i < n * n; i++) {
    cin >> order[i];
  }
  int seat[(n + 2) * (n + 2)];
  for (int i = 0; i < n + 2; i++) {
    for (int j = 0; j < n + 2; j++) {
      if (i == 0 || i == n + 1 || j == 0 || j == n + 1) {
        seat[i * (n + 2) + j] = 0;
      } else {
        seat[i * (n + 2) + j] = 1;
      }
    }
  }
  int bad_pair_num = 0;
  for (int i = 0; i < n * n; i++) {
    int row = (order[i] - 1) / n + 1;
    int col = (order[i] - 1) % n + 1;
    bad_pair_num += leave(row, col, 0, seat);
    seat[row * (n + 2) + col] = 0;
  }
  cout << bad_pair_num << endl;
  return 0;
}
