#include <bits/stdc++.h>
using namespace std;
struct state {
  int i, j, d;
};
int di[] = {-1, 0, +1, 0};
int dj[] = {0, -1, 0, +1};
bool used[505][505];
bool vis[505][505][505];
int dist[505][505];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N * N);
  for (long long i = (long long)(0); i < (long long)(N * N); i++) {
    cin >> A[i];
    A[i]--;
  }
  for (long long i = (long long)(0); i < (long long)(N); i++)
    for (long long j = (long long)(0); j < (long long)(N); j++) {
      vis[i][j][min({i, j, N - 1 - i, N - 1 - j}) + 1] = true;
      used[i][j] = true;
      dist[i][j] = min({i, j, N - 1 - i, N - 1 - j}) + 1;
    }
  long long res = 0;
  for (long long k = (long long)(0); k < (long long)(A.size()); k++) {
    int ni = A[k] / N, nj = A[k] % N;
    int ans = dist[ni][nj];
    ans--;
    res += (long long)ans;
    used[ni][nj] = false;
    queue<state> que;
    que.push((state){ni, nj, ans});
    vis[ni][nj][ans] = true;
    while (que.size()) {
      state s = que.front();
      que.pop();
      for (long long dir = (long long)(0); dir < (long long)(4); dir++) {
        int ci = s.i + di[dir], cj = s.j + dj[dir];
        if (ci < 0 || ci >= N || cj < 0 || cj >= N) continue;
        int cost = (used[ci][cj] ? 1 : 0);
        if (s.d + cost < N && !vis[ci][cj][s.d + cost]) {
          if (s.d + cost > dist[ci][cj]) continue;
          que.push((state){ci, cj, s.d + cost});
          vis[ci][cj][s.d + cost] = true;
          dist[ci][cj] =
              (dist[ci][cj] < s.d + cost ? dist[ci][cj] : s.d + cost);
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}
