#include <bits/stdc++.h>
using namespace std;
const int MAXN = 250010;
const int MAXM = 10010;
const int MAXINT = 2147483647;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, m, k;
int ans, tot, cnt;
int a[MAXM][MAXM];
int vis[MAXM][MAXM];
string s;
inline int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while ('0' > ch || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    s = (s << 3) + (s << 1) + ch - 48;
    ch = getchar();
  }
  return s * f;
}
int d[] = {-1, 1, n, -n};
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
struct data {
  int t, x, y;
  bool operator<(const data &c) const { return t > c.t; }
};
void Bfs(int s) {
  priority_queue<data> q;
  data cc;
  cc.x = (s - 1) / n + 1;
  cc.y = (s - 1) % n + 1;
  cc.t = 0;
  q.push(cc);
  memset(vis, 0, sizeof(vis));
  int res = INF;
  while (q.size()) {
    int x = q.top().x;
    int y = q.top().y;
    int t = q.top().t;
    q.pop();
    vis[x][y] = 1;
    if (x == 1 || x == n || y == 1 || y == n) {
      res = min(res, t);
      break;
    }
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (1 <= xx && xx <= n && 1 <= yy && yy <= n && !vis[xx][yy]) {
        cc.x = xx;
        cc.y = yy;
        cc.t = t + a[xx][yy];
        q.push(cc);
      }
    }
  }
  ans += res;
  return;
}
int p[MAXN];
int main() {
  int T;
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++) {
    scanf("%d", &p[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a[i][j] = 1;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    Bfs(p[i]);
    a[(p[i] - 1) / n + 1][(p[i] - 1) % n + 1] = 0;
  }
  printf("%d\n", ans);
  return 0;
}
