#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int maxm = 1e5 + 5;
int n, dis[maxn][maxn];
int x[maxm], y[maxm];
int mark[maxn][maxn];
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
inline void dfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    int nd = dis[x][y] + 1 - mark[xx][yy];
    if (dis[xx][yy] > nd) {
      dis[xx][yy] = nd;
      dfs(xx, yy);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++) {
    int tmp;
    scanf("%d", &tmp);
    x[i] = (tmp + n - 1) / n, y[i] = tmp - (x[i] - 1) * n;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1));
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    ans += dis[x[i]][y[i]] - 1;
    mark[x[i]][y[i]] = 1;
    for (int j = 0; j < 4; j++) {
      int xx = x[i] + dx[j], yy = y[i] + dy[j];
      dis[x[i]][y[i]] = min(dis[x[i]][y[i]], dis[xx][yy]);
    }
    dfs(x[i], y[i]);
  }
  printf("%d\n", ans);
  return 0;
}
