#include <bits/stdc++.h>
using namespace std;
int t, n, p[300010];
int ps = 0;
int dp[25010];
bool v[25010];
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    dp[i] = min(
        min((i - 1) / n, n - (i - 1) / n - 1),
        min(n - ((i % n == 0) ? n : (i % n)), (i % n == 0) ? n : (i % n) - 1));
    cin >> p[i];
  }
  for (int i = 1; i <= n * n; i++) {
    int x = p[i];
    v[p[i]] = true;
    ps += dp[x];
    queue<int> q;
    q.push(x);
    while (!q.empty()) {
      int f = q.front();
      q.pop();
      if (f + n <= n * n && dp[f + n] >= dp[f] + 1) {
        q.push(f + n);
        if (v[f] == true) {
          dp[f + n] = dp[f];
        } else {
          dp[f + n] = dp[f] + 1;
        }
      }
      if ((f - 1) % n >= 1 && (f - 1) >= 1 && dp[f - 1] >= dp[f] + 1) {
        q.push(f - 1);
        if (v[f] == true) {
          dp[f - 1] = dp[f];
        } else {
          dp[f - 1] = dp[f] + 1;
        }
      }
      if ((((f + 1) % n) != 1 && f + 1 <= n * n) && dp[f + 1] >= dp[f] + 1) {
        q.push(f + 1);
        if (v[f] == true) {
          dp[f + 1] = dp[f];
        } else {
          dp[f + 1] = dp[f] + 1;
        }
      }
      if (f - n >= 1 && dp[f - n] >= dp[f] + 1) {
        q.push(f - n);
        if (v[f] == true) {
          dp[f - n] = dp[f];
        } else {
          dp[f - n] = dp[f] + 1;
        }
      }
    }
  }
  cout << ps;
  return 0;
}
