#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int N, A, B, C, D, E, F, M, K, L, X, Y, Z, H, W, sum = 0, num = 0,
                                                             flag = 0;
  string S, T;
  cin >> N;
  long long int d[4] = {-1, 1, N, 0 - N};
  vector<long long int> P(N * N, 0);
  vector<long long int> G(N * N, 0);
  vector<bool> Fl(N * N, 0);
  for (long long int i = 0; i < N * N; i++) cin >> P[i];
  vector<long long int> dist(N * N, 0);
  for (long long int i = 0; i < N * N; i++) {
    A = i % N;
    B = i / N;
    dist[i] = min(min(A, N - A - 1), min(B, N - B - 1));
  }
  for (long long int i = 0; i < N * N; i++) {
    sum += dist[P[i] - 1];
    queue<long long int> que;
    que.push(P[i] - 1);
    vector<bool> sc(N * N, 1);
    while (!que.empty()) {
      auto v = que.front();
      que.pop();
      for (long long int j = 0; j < 4; j++) {
        auto nv = v + d[j];
        if (0 > nv || nv >= N * N) continue;
        if (sc[nv]) continue;
        sc[nv] = 1;
        dist[nv] = (((dist[nv]) > (dist[v])) ? (dist[v]) : (dist[nv]));
        if (Fl[nv]) que.push(nv);
      }
    }
    Fl[P[i] - 1] = 1;
  }
  cout << sum << endl;
}
