#include <bits/stdc++.h>
using namespace std;
int n, dis[250010], x, ans, a[250010][5], an[250010], q[250010], l, r,
    bo[250010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      int nw = (i - 1) * n + j;
      dis[nw] = min(min(i - 1, n - i), min(j - 1, n - j)), bo[nw] = 1;
      if (i != 1) a[nw][++an[nw]] = nw - n;
      if (i != n) a[nw][++an[nw]] = nw + n;
      if (j != 1) a[nw][++an[nw]] = nw - 1;
      if (j != n) a[nw][++an[nw]] = nw + 1;
    }
  for (int qaq = 1; qaq <= n * n; qaq++) {
    scanf("%d", &x), ans += dis[x];
    q[l = r = 1] = x, bo[x] = 0;
    while (l <= r) {
      x = q[l++];
      for (int i = 1; i <= an[x]; i++)
        if (dis[x] + bo[x] < dis[a[x][i]])
          dis[a[x][i]] = dis[x] + bo[x], q[++r] = a[x][i];
    }
  }
  printf("%d\n", ans);
  return 0;
}
