#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  int N, count, ans;
  cin >> N;
  ans = 0;
  int seat[N][N];
  string beat[N][N];
  int C[N * N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      seat[i][j] = i + j;
      beat[i][j] = "NOT";
    }
  }
  int B[N * N];
  for (int i = 0; i < N * N; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < N * N; i++) {
    count = 0;
    queue<pair<int, int>> que;
    que.push(make_pair(B[i] / N + 1, B[i] % N));
    seat[B[i] / N][B[i] % N] = 0;
    while (!que.empty()) {
      pair<int, int> current_pos = que.front();
      int x = current_pos.first;
      int y = current_pos.second;
      que.pop();
      for (int direction = 0; direction < 4; direction++) {
        int next_x = x + dx[direction];
        int next_y = y + dy[direction];
        if (next_x < 0 || next_x >= N || next_y < 0 || next_y >= N) {
          while (!que.empty()) {
            que.pop();
          }
          break;
        }
        if (seat[next_x][next_y] != 0) {
          if (direction == 3) {
            count++;
          } else {
            continue;
          }
        }
        if (beat[next_x][next_y] == "NOT") {
          que.push(make_pair(next_x, next_y));
          beat[next_x][next_y] = "SEEN";
        }
      }
    }
    ans += count;
  }
  cout << ans << endl;
}
