#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  vector<vector<int> > dist(n, vector<int>(n));
  vector<vector<bool> > occupied(n, vector<bool>(n, true));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
    }
  }
  vector<int> bfs;
  bfs.reserve(n * n);
  const int mx[4] = {0, 1, 0, -1};
  const int my[4] = {1, 0, -1, 0};
  for (int _ = 0; _ < n * n; _++) {
    int cur;
    cin >> cur;
    cur--;
    int sx = cur / n, sy = cur % n;
    occupied[sx][sy] = false;
    dist[sx][sy]--;
    ans += dist[sx][sy];
    bfs.push_back(cur);
    for (int s = 0; s < int(bfs.size()); s++) {
      int v = bfs[s];
      int x = v / n, y = v % n;
      for (int d = 0; d < 4; d++) {
        int nx = x + mx[d], ny = y + my[d];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          int nd = occupied[nx][ny] + dist[x][y];
          if (nd < dist[nx][ny]) {
            dist[nx][ny] = nd;
            bfs.push_back(nx * n + ny);
          }
        }
      }
    }
  }
  cout << ans << '\n';
}
