#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int n;
int a[N * N];
bool st[N][N];
int ans;
int d[N][N];
bool go[N][N];
deque<pair<int, int> > que;
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
int bfs(int first, int second) {
  que.clear();
  memset(d, 0x3f, sizeof d);
  memset(go, false, sizeof go);
  d[first][second] = 0;
  que.push_back({first, second});
  while (que.size()) {
    auto t = que.front();
    que.pop_front();
    if (go[t.first][t.second]) continue;
    go[t.first][t.second] = true;
    if (t.first == 0 || t.first == n - 1 || t.second == 0 ||
        t.second == n - 1) {
      return d[t.first][t.second];
    }
    for (int i = 0; i < 4; i++) {
      int a = t.first + dx[i], b = t.second + dy[i];
      if (go[a][b]) continue;
      if (st[a][b]) {
        d[a][b] = min(d[a][b], d[t.first][t.second]);
        que.push_front({a, b});
      } else {
        d[a][b] = min(d[a][b], d[t.first][t.second] + 1);
        que.push_back({a, b});
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n * n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < n * n; i++) {
    int p = a[i] - 1;
    int px = p / n, py = p % n;
    ans += bfs(px, py);
    st[px][py] = true;
  }
  cout << ans << endl;
  return 0;
}
