#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int n, a[N][N], x, ans;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
queue<pair<pair<int, int>, int>> q;
set<pair<int, int>> s;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      a[i][j] = 1;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    cin >> x;
    int r = (x - 1) / n + 1;
    int c = x - (r - 1) * n;
    int le = 0, ri = n * n, now = -1;
    while (le <= ri) {
      int mid = (le + ri) / 2;
      bool ok = false;
      q.push({{r, c}, mid});
      while (!q.empty()) {
        auto cur = q.front();
        q.pop();
        int nowr = cur.first.first;
        int nowc = cur.first.second;
        int sisa = cur.second;
        if (nowr == 1 || nowr == n || nowc == 1 || nowc == n) {
          ok = true;
          break;
        }
        for (int j = 0; j < 4; j++) {
          int nxr = nowr + dr[j];
          int nxc = nowc + dc[j];
          if (!s.count({nxr, nxc}) && sisa >= a[nxr][nxc]) {
            q.push({{nxr, nxc}, sisa - a[nxr][nxc]});
            s.insert({nxr, nxc});
          }
        }
      }
      while (!q.empty()) q.pop();
      s.clear();
      if (ok) {
        now = mid;
        ri = mid - 1;
      } else {
        le = mid + 1;
      }
    }
    assert(now != -1);
    ans += now;
    a[r][c] = 0;
  }
  cout << ans << "\n";
}
