#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int N;
  cin >> N;
  vector<int> P(N * N);
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
    P[i]--;
  }
  vector<vector<int>> dist(N, vector<int>(N, -1));
  vector<vector<int>> state(N, vector<int>(N, -1));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dist[i][j] = min({i, j, N - i - 1, N - j - 1});
      state[i][j] = 1;
    }
  }
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  auto Inside = [&](int x, int y) {
    return 0 <= x && x < N && 0 <= y && y < N;
  };
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int x = P[i] / N, y = P[i] % N;
    ans += dist[x][y];
    state[x][y] = 0;
    deque<pair<pair<int, int>, int>> dq;
    dq.push_back({{x, y}, dist[x][y]});
    while (!dq.empty()) {
      int r = dq.front().first.first;
      int c = dq.front().first.second;
      int dt = dq.front().second;
      dq.pop_front();
      if (dist[x][y] != dt) continue;
      for (int d = 0; d < 4; d++) {
        int nr = r + dx[d];
        int nc = c + dy[d];
        int w = state[r][c];
        if (Inside(nr, nc) && dist[nr][nc] > dist[r][c] + w) {
          dist[nr][nc] = dist[r][c] + w;
          if (w) {
            dq.push_back({{nr, nc}, dist[nr][nc]});
          } else {
            dq.push_front({{nr, nc}, dist[nr][nc]});
          }
        }
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
