#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long int INF = 4611686018427387903;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
  int n;
  cin >> n;
  vector<long long int> p(n * n);
  for (int i = 0; i < n * n; i++) cin >> p[i];
  vector<vector<bool>> f(n, vector<bool>(n, 1));
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    p[i]--;
    int sx = p[i] % n, sy = p[i] / n;
    bool ok = 0;
    f[sx][sy] = 0;
    priority_queue<pair<int, pair<long long int, long long int>>> q;
    q.push({0, {sx, sy}});
    vector<vector<long long int>> d(n, vector<long long int>(n, INF));
    d[sx][sy] = 0;
    while (!q.empty()) {
      int c = -q.top().first;
      int x = q.top().second.first;
      int y = q.top().second.second;
      q.pop();
      if (c > d[x][y]) continue;
      for (int j = 0; j < 4; j++) {
        int tox = x + dx[j], toy = y + dy[j];
        if (tox >= 0 && tox < n && toy >= 0 && toy < n) {
          if (c + f[tox][toy] < d[tox][toy]) {
            d[tox][toy] = c + f[tox][toy];
            q.push({-(c + f[tox][toy]), {tox, toy}});
          }
        } else {
          ok = 1;
          ans += c;
          break;
        }
      }
      if (ok) break;
    }
  }
  cout << ans << endl;
  return 0;
}
