#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int maxc = 3e3 + 10;
const int N = 2e5 + 10;
struct poi {
  int x, y, go;
  int val;
  bool operator<(const poi& t) const { return val > t.val; }
};
bool vis[510][510];
bool ed[510][510];
int mp[510][510];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n;
bool ck(int x, int y) {
  return x >= 1 && x <= n && y >= 1 && y <= n && !vis[x][y];
}
int a[510];
int main() {
  scanf("%d", &n);
  int id = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) mp[i][j] = id++;
  }
  for (int i = 1; i <= n * n; i++) scanf("%d", &a[i]);
  priority_queue<poi> pq;
  poi now, nex;
  long long ans = 0;
  for (int i = 1; i <= n * n; i++) {
    while (pq.size()) pq.pop();
    now.x = (a[i] - 1) / n + 1, now.y = (a[i] % n == 0 ? n : a[i] % n),
    now.val = 0;
    now.go = mp[now.x][now.y];
    ed[now.x][now.y] = 1;
    memset(vis, 0, sizeof(vis));
    pq.push(now);
    while (pq.size()) {
      now = pq.top();
      pq.pop();
      if ((now.x == 1 || now.x == n) || (now.y == 1 || now.y == n)) {
        ans += now.val;
        break;
      }
      for (int k = 0; k < 4; k++) {
        int nx = now.x + dx[k], ny = now.y + dy[k], nval = now.val,
            ngo = now.go;
        if (!ck(nx, ny)) continue;
        vis[nx][ny] = 1;
        if (ed[nx][ny] == 0) nval++;
        nex.x = nx, nex.y = ny, nex.go = ngo, nex.val = nval;
        pq.push(nex);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
