#include <bits/stdc++.h>
using namespace std;
int const maxn = 505;
int used[maxn][maxn], dist[maxn][maxn];
pair<int, int> Q[3 * maxn * maxn];
vector<pair<int, int> > go = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
pair<int, int> was[maxn * maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, x;
  long long ans = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) used[i][j] = 1, dist[i][j] = n * n + 1;
  for (int i = 1; i <= n * n; ++i) {
    cin >> x;
    int L = maxn * maxn + 10, R = L, ptr = 0;
    Q[L] = {(x - 1) / n + 1, (x - 1) % n + 1};
    was[ptr++] = Q[L];
    dist[(x - 1) / n + 1][(x - 1) % n + 1] = 0;
    while (L <= R) {
      int pos_i = Q[L].first, pos_j = Q[L].second, d = dist[pos_i][pos_j];
      ++L;
      if (pos_i == 1 || pos_j == 1 || pos_i == n || pos_j == n) {
        ans += (long long)dist[pos_i][pos_j];
        break;
      }
      for (auto key : go) {
        pos_i += key.first, pos_j += key.second;
        if (dist[pos_i][pos_j] > d + used[pos_i][pos_j]) {
          assert(dist[pos_i][pos_j] == n * n + 1);
          dist[pos_i][pos_j] = d + used[pos_i][pos_j];
          was[ptr++] = {pos_i, pos_j};
          if (used[pos_i][pos_j])
            Q[R++] = {pos_i, pos_j};
          else
            L--, Q[L] = {pos_i, pos_j};
        }
        pos_i -= key.first, pos_j -= key.second;
      }
    }
    for (int j = 0; j < ptr; ++j) dist[was[j].first][was[j].second] = n * n + 1;
    used[(x - 1) / n + 1][(x - 1) % n + 1] = 0;
  }
  cout << ans << '\n';
  return 0;
}
