#include <bits/stdc++.h>
using namespace std;
int closerl[505][505], closecl[505][505];
int closerr[505][505], closecr[505][505];
int dis[505][505];
pair<int, int> f[505][505];
int ok[505][505];
pair<int, int> Find(pair<int, int> p) {
  if (f[p.first][p.second] == p) return p;
  return f[p.first][p.second] = Find(f[p.first][p.second]);
}
int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};
int main() {
  int n;
  scanf("%d", &n);
  memset(dis, 0, sizeof(dis));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      closerl[i][j] = 0;
      closerr[i][j] = n + 1;
      closecl[i][j] = 0;
      closecr[i][j] = n + 1;
      ok[i][j] = 1;
    }
  }
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= n + 1; j++) {
      f[i][j] = make_pair(i, j);
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int P;
    scanf("%d", &P);
    P--;
    int first = P / n + 1;
    int second = P % n + 1;
    int d = 1e9;
    for (int j = 1; j <= n; j++) {
      pair<int, int> p = Find(make_pair(j, closerl[j][second]));
      d = min(d, abs(second - closerl[j][second]) + abs(first - j) - 1 +
                     dis[p.first][p.second]);
      p = Find(make_pair(closecl[first][j], j));
      d = min(d, abs(first - closecl[first][j]) + abs(second - j) - 1 +
                     dis[p.first][p.second]);
      p = Find(make_pair(j, closerr[j][second]));
      d = min(d, abs(second - closerr[j][second]) + abs(first - j) - 1 +
                     dis[p.first][p.second]);
      p = Find(make_pair(closecr[first][j], j));
      d = min(d, abs(first - closecr[first][j]) + abs(second - j) - 1 +
                     dis[p.first][p.second]);
    }
    ans += d;
    dis[first][second] = d;
    ok[first][second] = 0;
    for (int j = 0; j < 4; j++) {
      int xx = X[j] + first, yy = Y[j] + second;
      if (!ok[xx][yy]) {
        pair<int, int> p = Find(make_pair(xx, yy));
        pair<int, int> q = Find(make_pair(first, second));
        f[p.first][p.second] = q;
        dis[q.first][q.second] =
            min(dis[q.first][q.second], dis[p.first][p.second]);
      }
    }
    for (int j = 1; j <= n; j++) {
      if (j >= second) {
        if (abs(closerl[first][j] - j) > abs(second - j))
          closerl[first][j] = second;
      } else {
        if (abs(closerr[first][j] - j) > abs(second - j))
          closerr[first][j] = second;
      }
      if (j >= first) {
        if (abs(closecl[j][second] - j) > abs(first - j))
          closecl[j][second] = first;
      } else {
        if (abs(closecr[j][second] - j) > abs(first - j))
          closecr[j][second] = first;
      }
    }
  }
  printf("%d\n", ans);
}
