#include <bits/stdc++.h>
using namespace std;
int next1[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int s[300010];
int vis[550][550];
int jud[550][550];
int cnt = 0;
int minn;
int n;
void dfs(int x, int y, int step) {
  jud[x][y] = 1;
  if (x == 1 || y == 1 || x == n || y == n) {
    minn = min(minn, step);
    return;
  }
  for (int i = 0; i < 4; i++) {
    int xx = x + next1[i][0];
    int yy = y + next1[i][1];
    if (!jud[xx][yy]) {
      jud[xx][yy] = 1;
      if (vis[xx][yy] == 0) {
        dfs(xx, yy, step);
      } else {
        dfs(xx, yy, step + 1);
      }
      jud[xx][yy] = 0;
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n * n; i++) cin >> s[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) vis[i][j] = (i - 1) * n + j;
  }
  for (int i = 1; i <= n * n; i++) {
    int x;
    if (s[i] % n == 0) {
      x = s[i] / n;
    } else {
      x = (s[i] + n) / n;
    }
    int y = s[i] % n;
    if (y == 0) y = n;
    if (x == 1 || y == 1 || x == n || y == n) {
      vis[x][y] = 0;
      continue;
    }
    minn = 1000000007;
    memset(jud, 0, sizeof(jud));
    dfs(x, y, 0);
    vis[x][y] = 0;
    cnt += minn;
  }
  cout << cnt << endl;
  return 0;
}
