#include <bits/stdc++.h>
using namespace std;
int dist[507][507], ind[507][507], rev[507 * 507], n;
bool vi[507][507];
bool inside(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= n; }
void upd(int x, int y, int nd) {
  int add = (nd >= ind[x][y]) ? 0 : 1;
  if (inside(x + 1, y) && dist[x + 1][y] > dist[x][y] + add) {
    dist[x + 1][y] = dist[x][y] + add;
    upd(x + 1, y, nd);
  }
  if (inside(x - 1, y) && dist[x - 1][y] > dist[x][y] + add) {
    dist[x - 1][y] = dist[x][y] + add;
    upd(x - 1, y, nd);
  }
  if (inside(x, y + 1) && dist[x][y + 1] > dist[x][y] + add) {
    dist[x][y + 1] = dist[x][y] + add;
    upd(x, y + 1, nd);
  }
  if (inside(x, y - 1) && dist[x][y - 1] > dist[x][y] + add) {
    dist[x][y - 1] = dist[x][y] + add;
    upd(x, y - 1, nd);
  }
}
int main() {
  int rez = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++) {
    scanf("%d", &rev[i]);
    ind[rev[i] % n][(rev[i] - 1) / n + 1] = i;
  }
  queue<int> q;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i == 1 || i == n || j == 1 || j == n) {
        vi[i][j] = true;
        q.push(ind[i][j]);
      }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int x = rev[u] % n, y = (rev[u] - 1) / n + 1;
    if (inside(x + 1, y) && !vi[x + 1][y]) {
      vi[x + 1][y] = true;
      q.push(ind[x + 1][y]);
      dist[x + 1][y] = dist[x][y] + 1;
    }
    if (inside(x - 1, y) && !vi[x - 1][y]) {
      vi[x - 1][y] = true;
      q.push(ind[x - 1][y]);
      dist[x - 1][y] = dist[x][y] + 1;
    }
    if (inside(x, y + 1) && !vi[x][y + 1]) {
      vi[x][y + 1] = true;
      q.push(ind[x][y + 1]);
      dist[x][y + 1] = dist[x][y] + 1;
    }
    if (inside(x, y - 1) && !vi[x][y - 1]) {
      vi[x][y - 1] = true;
      q.push(ind[x][y - 1]);
      dist[x][y - 1] = dist[x][y] + 1;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    int x = rev[i] % n, y = (rev[i] - 1) / n + 1;
    rez += dist[x][y];
    upd(x, y, i);
  }
  printf("%d", rez);
}
