#include <bits/stdc++.h>
using namespace std;
bool vis[505][505];
int dis[505][505];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    int x = i / n, y = i % n;
    dis[x][y] = min({x, y, n - 1 - x, n - 1 - y});
  }
  memset(vis, false, sizeof vis);
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    int z;
    cin >> z;
    z--;
    int X = z / n, Y = z % n;
    ans += dis[X][Y];
    vis[X][Y] = true;
    queue<pair<int, int>> q;
    q.push({X, Y});
    while (!q.empty()) {
      int x = q.front().first, y = q.front().second;
      q.pop();
      for (int j = 0; j < 4; j++) {
        if (x + dx[j] >= 0 && x + dx[j] < n && y + dx[y] >= 0 &&
            y + dx[y] < n &&
            dis[x + dx[j]][y + dy[j]] > dis[x][y] + 1 - (int)(vis[x][y])) {
          dis[x + dx[j]][y + dy[j]] = dis[x][y] + 1 - (int)(vis[x][y]);
          q.push({x + dx[j], y + dy[j]});
        }
      }
    }
  }
  cout << ans;
}
