#include <bits/stdc++.h>
using namespace std;
int const veci[4] = {0, -1, 0, 1};
int const vecj[4] = {-1, 0, 1, 0};
int N;
int P[25000];
int G[500][500];
int S[500][500];
int main() {
  cin >> N;
  for (int i = 0; i < N * N; i++) cin >> P[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      G[i][j] = min(min(i, j), min(N - i - 1, N - j - 1));
    }
  }
  for (int i = 0; i < N; i++) fill(S[i], S[i] + N, 1);
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int i1 = (P[i] - 1) / N;
    int j1 = (P[i] - 1) % N;
    ans += G[i1][j1];
    S[i1][j1] = 0;
    queue<pair<int, int> > que;
    que.push(pair<int, int>(i1, j1));
    while (!que.empty()) {
      pair<int, int> p = que.front();
      que.pop();
      for (int k = 0; k < 4; k++) {
        int i2 = p.first + veci[k];
        int j2 = p.second + vecj[k];
        if (0 <= i2 && i2 < N && 0 <= j2 && j2 < N) {
          if (S[p.first][p.second] == 0 && G[p.first][p.second] < G[i2][j2]) {
            G[i2][j2] = G[p.first][p.second];
            que.push(pair<int, int>(i2, j2));
          }
          if (S[p.first][p.second] == 1 &&
              G[p.first][p.second] + 1 < G[i2][j2]) {
            G[i2][j2] = G[p.first][p.second] + 1;
            que.push(pair<int, int>(i2, j2));
          }
        }
      }
    }
  }
  cout << ans << endl;
}
