#include <bits/stdc++.h>
using namespace std;
int n, x, y, t, ans = 0;
int p[505][505];
bool q[505][505] = {0};
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int min4(int a, int b, int c, int d) {
  if (b < a) a = b;
  if (c < a) a = c;
  if (d < a) a = d;
  return a;
}
void dfs(int x, int y, int dis, bool f[505][505]) {
  p[x][y] = min(p[x][y], dis);
  cout << x << y << endl;
  if (!q[x][y]) return;
  int xx, yy;
  for (int i = 0; i < 4; i++) {
    xx = x + dx[i];
    yy = y + dy[i];
    if (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
      if (f[xx][yy] == 0) {
        f[xx][yy] = 1;
        dfs(xx, yy, dis, f);
      }
    }
  }
  return;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) p[i][j] = min4(i - 1, j - 1, n - i, n - j);
  for (int k = 1; k <= n * n; k++) {
    cin >> t;
    y = t % n;
    if (y == 0)
      x = t / n;
    else
      x = t / n + 1;
    ans += p[x][y];
    q[x][y] = 1;
    bool f[505][505] = {0};
    f[x][y] = 1;
    dfs(x, y, p[x][y], f);
  }
  cout << ans;
  return 0;
}
