#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
long long jou(long long N, long long P) {
  if (P == 0) return 1;
  if (P % 2 == 0) {
    long long t = jou(N, P / 2);
    return t * t % 1000000007;
  }
  return (N * jou(N, P - 1)) % 1000000007;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  int n;
  cin >> n;
  if (n == 2) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  vector<vector<bool>> p(n, vector<bool>(n, true));
  vector<vector<int>> dist(n, vector<int>(n));
  deque<pair<int, int>> que;
  for (int i = 0; i < (int)(n * n); i++) {
    int x;
    cin >> x;
    int a = (x - 1) / n;
    int b = (x - 1) % n;
    p[a][b] = false;
    if (a == 0 || a == n - 1 || b == 0 || b == n - 1) continue;
    for (int i = 0; i < (int)(n); i++) {
      for (int j = 0; j < (int)(n); j++) {
        dist[i][j] = -1;
      }
    }
    dist[a][b] = 0;
    pair<int, int> q = make_pair(a, b);
    que.push_back(q);
    while (!que.empty()) {
      pair<int, int> v = que.front();
      que.pop_front();
      for (int j = 0; j < (int)(4); j++) {
        int nh = v.first + dx[j];
        int nw = v.second + dy[j];
        if (nh < 0 || nh >= n || nw < 0 || nw >= n) continue;
        if (dist[nh][nw] != -1) continue;
        if (p[nh][nw]) {
          dist[nh][nw] = dist[v.first][v.second] + 1;
          pair<int, int> r = make_pair(nh, nw);
          que.push_back(r);
        } else {
          dist[nh][nw] = dist[v.first][v.second];
          pair<int, int> r = make_pair(nh, nw);
          que.push_front(r);
        }
      }
    }
    int sho = 1000000;
    for (int j = 0; j < (int)(n); j++) {
      sho = min(sho, dist[0][j]);
      sho = min(sho, dist[n - 1][j]);
      sho = min(sho, dist[j][0]);
      sho = min(sho, dist[j][n - 1]);
    }
    ans += sho;
  }
  cout << ans << endl;
}
