#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 505;
int p[N * N];
int vis[N][N];
int dis[N][N];
bool mk[N][N];
int n, ans;
int dx[]{1, -1, 0, 0};
int dy[]{0, 0, 1, -1};
void dij(int x, int y, int f) {
  ans += dis[x][y];
  queue<pair<int, pair<int, int>>> que;
  que.push({dis[x][y], {x, y}});
  vis[x][y] = f;
  mk[x][y] = true;
  while (!que.empty()) {
    auto [d, p] = que.front();
    auto [x, y] = p;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx < 0 || yy < 0 || xx >= n || yy >= n || vis[xx][yy] == f) continue;
      vis[xx][yy] = f;
      int v = d + !mk[x][y];
      if (v < dis[xx][yy]) {
        dis[xx][yy] = v;
        que.push({v, {xx, yy}});
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> p[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dis[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
    }
  }
  for (int i = 0; i < n * n; i++) {
    int t = p[i] - 1;
    dij(t / n, t % n, i + 1);
  }
  cout << ans << endl;
  return 0;
}
