#include <bits/stdc++.h>
using namespace std;
int N;
int P[250000];
bool seated[500][500];
int unliked[250000];
void show_now() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << seated[i][j] << " ";
    }
    cout << endl;
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
    seated[i / N][i % N] = true;
  }
  for (int p = 0; p < N * N; p++) {
    int pi = (P[p] - 1) / N;
    int pj = (P[p] - 1) % N;
    seated[pi][pj] = false;
    int visited[N][N];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        visited[i][j] = (1e9);
      }
    }
    visited[pi][pj] = 0;
    unliked[p] = (1e9);
    queue<int> que;
    que.push(P[p]);
    while (!que.empty()) {
      int Q = que.front();
      que.pop();
      int qi = (Q - 1) / N;
      int qj = (Q - 1) % N;
      if (qi == 0 || qi == N - 1 || qj == 0 || qj == N - 1) {
        unliked[p] = min(unliked[p], visited[qi][qj]);
        continue;
      }
      int move[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
      int next[4][2];
      for (int i = 0; i < 4; i++) {
        next[i][0] = qi + move[i][0];
        next[i][1] = qj + move[i][1];
        if (visited[qi][qj] + seated[next[i][0]][next[i][1]] <
            visited[next[i][0]][next[i][1]]) {
          visited[next[i][0]][next[i][1]] =
              visited[qi][qj] + seated[next[i][0]][next[i][1]];
          que.push(N * next[i][0] + next[i][1] + 1);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    ans += unliked[i];
  }
  cout << ans << endl;
}
