#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
using pii = pair<int, int>;
int main() {
  int N;
  cin >> N;
  vector<vector<bool>> out(N, vector<bool>(N, false));
  vector<vector<int>> step(N, vector<int>(N, N));
  for (int y = 0; y < N; ++y)
    for (int x = 0; x < N; ++x) {
      step[y][x] = min(step[y][x], x);
      step[y][x] = min(step[y][x], N - 1 - x);
      step[y][x] = min(step[y][x], y);
      step[y][x] = min(step[y][x], N - 1 - y);
    }
  int ans = 0;
  for (int i = 0; i < N * N; ++i) {
    int P;
    cin >> P;
    --P;
    int sx = P % N;
    int sy = P / N;
    int val = step[sy][sx];
    ans += val;
    out[sy][sx] = true;
    deque<pii> que;
    que.push_back({sx, sy});
    while (!que.empty()) {
      auto [x, y] = que.front();
      que.pop_front();
      for (int d = 0; d < 4; ++d) {
        int nx = x + dx[d], ny = y + dy[d];
        if (nx < 0 or nx >= N or ny < 0 or ny >= N) continue;
        if (step[ny][nx] > val + (out[y][x] ? 0 : 1)) {
          step[ny][nx] = val + (out[y][x] ? 0 : 1);
          if (out[y][x])
            que.push_back({nx, ny});
          else
            que.push_front({nx, ny});
        }
      }
    }
  }
  cout << ans << endl;
}
