#include <bits/stdc++.h>
using namespace std;
int n;
bool a[555][555];
bool b[555][555];
int memo[555][555];
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int solve(int y, int x) {
  queue<pair<int, int> > que;
  int cost[555][555];
  int ret = 1 << 28;
  fill(cost[0], cost[555], 1 << 28);
  cost[y][x] = 0;
  que.push(pair<int, int>(y, x));
  while (que.size()) {
    int ny = que.front().first, nx = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int xx = dx[i] + nx, yy = dy[i] + ny;
      if (xx == 0) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }
      if (yy == 0) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }
      if (xx > n) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }
      if (yy > n) {
        ret = min(ret, cost[ny][nx]);
        continue;
      }
      if (cost[yy][xx] > cost[ny][nx] + a[yy][xx]) {
        cost[yy][xx] = cost[ny][nx] + a[yy][xx];
        que.push(pair<int, int>(yy, xx));
      }
    }
  }
  return ret;
}
int main() {
  int ans = 0;
  cin >> n;
  for (int i = 0; i < 555 * 555; i++) a[i / 555][i % 555] = 1;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int y = p / n, x = p % n;
    y++;
    x++;
    a[y][x] = 0;
    fill(memo[0], memo[555], 1 << 28);
    ans += solve(y, x);
  }
  cout << ans << endl;
}
