#include <bits/stdc++.h>
using namespace std;
const long long LINF = 1LL << 60;
const int INF = 1001001001;
const int MOD = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int main() {
  int n;
  cin >> n;
  vector<vector<int>> field(n, vector<int>(n));
  for (int i = 0; i < (n); i++) {
    int a = i * n;
    for (int j = 0; j < (n); j++) {
      field[i][j] = a;
      a++;
    }
  }
  int num = n * n;
  vector<int> p(num);
  for (int i = 0; i < (num); i++) {
    int a;
    cin >> a;
    a--;
    p[i] = a;
  }
  vector<bool> check(num, true);
  long long ans = 0;
  for (int i = 0; i < (num); i++) {
    int sy = p[i] / n;
    int sx = p[i] % n;
    check[p[i]] = false;
    if (p[i] % n == 0 || p[i] % n == n - 1 || p[i] / n == 0 ||
        p[i] / n == n - 1)
      continue;
    vector<vector<int>> dist(n, vector<int>(n, INF));
    queue<pair<int, int>> q;
    auto update = [&](int a, int b, int x) {
      dist[a][b] = min(x, dist[a][b]);
      q.push(pair<int, int>(a, b));
    };
    update(sy, sx, 0);
    while (!q.empty()) {
      int ny = q.front().first;
      int nx = q.front().second;
      q.pop();
      for (int dir = 0; dir < (4); dir++) {
        int nj = ny + dy[dir], ni = nx + dx[dir];
        if (nj < 0 || nj >= n) {
          if (dist[ny][ni] == 0)
            break;
          else
            continue;
        }
        if (ni < 0 || ni >= n) {
          if (dist[nj][nx] == 0)
            break;
          else
            continue;
        }
        if (check[field[nj][ni]]) {
          if (dist[ny][nx] + 1 >= dist[nj][ni])
            continue;
          else
            update(nj, ni, dist[ny][nx] + 1);
        } else {
          if (dist[ny][nx] >= dist[nj][ni])
            continue;
          else
            update(nj, ni, dist[ny][nx]);
        }
      }
    }
    int tot = INF;
    for (int i = 0; i < (n); i++) {
      tot = min(dist[0][i], tot);
      tot = min(dist[n - 1][i], tot);
      tot = min(dist[i][0], tot);
      tot = min(dist[i][n - 1], tot);
    }
    ans += tot;
  }
  cout << ans << endl;
  return 0;
}
