#include <bits/stdc++.h>
using namespace std;
const int nsz = int(5e2), msz = nsz * nsz;
const int dx[] = {-1, 0, 0, +1}, dy[] = {0, -1, +1, 0};
bool vis[nsz + 5][nsz + 5];
int n, m, d[nsz + 5][nsz + 5];
long long ans;
pair<int, int> fm[msz + 5];
bool inline upd(int &a, int b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void inline bfs(int sx, int sy) {
  static int qx[msz + 5], qy[msz + 5];
  int ql = 0, qr = 0;
  qx[qr++] = sx, qy[qr++] = sy;
  for (; ql != qr;) {
    int x = qx[ql++], y = qy[ql++];
    for (int dr = 0; dr < 4; ++dr) {
      int cx = x + dx[dr], cy = y + dy[dr];
      if (cx < 1 || cy < 1 || cx > n || cy > n ||
          !upd(d[cx][cy], d[x][y] + !vis[x][y]))
        continue;
      qx[qr++] = cx, qy[qr++] = cy;
    }
  }
}
int main() {
  scanf("%d", &n);
  m = n * n;
  int s = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      fm[++s] = make_pair(i, j);
      d[i][j] = n;
      upd(d[i][j], i - 1);
      upd(d[i][j], j - 1);
      upd(d[i][j], n - i);
      upd(d[i][j], n - j);
    }
  for (int i = 1; i <= m; ++i) {
    int id, x, y;
    scanf("%d", &id);
    x = fm[id].first, y = fm[id].second;
    ans += d[x][y];
    vis[x][y] = 1;
    bfs(x, y);
  }
  printf("%lld\n", ans);
}
