#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long big = 1e18;
const double PI = 2 * asin(1);
int main() {
  int N;
  cin >> N;
  int P[N * N];
  for (int i = 0; i < N * N; ++i) {
    cin >> P[i];
    P[i]--;
  }
  int DP[N][N];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      DP[i][j] = min(i, min(j, min(N - 1 - i, N - 1 - j)));
    }
  }
  bool oru[N][N];
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      oru[i][j] = true;
    }
  }
  int ans = 0;
  queue<pair<int, int> > que;
  int x, y, cost;
  pair<int, int> state;
  for (int i = 0; i < N * N; ++i) {
    ans += DP[P[i] / N][P[i] % N];
    oru[P[i] / N][P[i] % N] = false;
    que.push(make_pair(P[i], DP[P[i] / N][P[i] % N]));
    while (!que.empty()) {
      state = que.front();
      que.pop();
      x = state.first / N;
      y = state.first % N;
      cost = state.second;
      if (DP[x][y] < cost) continue;
      DP[x][y] = cost;
      if (oru[x][y]) cost++;
      if (x > 0 && DP[x - 1][y] > cost)
        que.push(make_pair((x - 1) * N + y, cost));
      if (y > 0 && DP[x][y - 1] > cost)
        que.push(make_pair(x * N + (y - 1), cost));
      if (x + 1 < N && DP[x + 1][y] > cost)
        que.push(make_pair((x + 1) * N + y, cost));
      if (y + 1 < N && DP[x][y + 1] > cost)
        que.push(make_pair(x * N + (y + 1), cost));
    }
  }
  cout << ans << endl;
}
