#include <bits/stdc++.h>
using namespace std;
int n, nn, ans, a[105][105];
int p[105 * 105], dis[105][105];
int dox[4] = {0, 1, 0, -1}, doy[4] = {1, 0, -1, 0};
int q[105], h, t;
template <class T>
inline T Max(T a, T b) {
  return a > b ? a : b;
}
template <class T>
inline T Min(T a, T b) {
  return a < b ? a : b;
}
void dfs(int sx, int sy) {
  h = 1;
  t = 0;
  q[++t] = (sx - 1) * n + sy;
  while (h <= t) {
    int id = q[h++];
    int x = (id - 1) / n + 1, y = (id - 1) % n + 1;
    for (int k = 0; k < 4; ++k) {
      int dx = x + dox[k], dy = y + doy[k];
      if (dx < 1 || dy < 1 || dx > n || dy > n) continue;
      if (dis[dx][dy] > dis[x][y] + a[x][y]) {
        dis[dx][dy] = dis[x][y] + a[x][y];
        q[++t] = (dx - 1) * n + dy;
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  nn = n * n;
  for (int i = 1; i <= nn; ++i) scanf("%d", p + i);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      a[i][j] = 1;
      dis[i][j] = Min(Min(i - 1, n - i), Min(j - 1, n - j));
    }
  }
  for (int i = 1; i <= nn; ++i) {
    int id = p[i];
    int x = (id - 1) / n + 1, y = (id - 1) % n + 1;
    ans += dis[x][y];
    a[x][y] = 0;
    dfs(x, y);
  }
  printf("%d\n", ans);
  return 0;
}
