#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long qpow(long long a, long long n) {
  long long r = 1 % P;
  for (a %= P; n; a = a * a % P, n >>= 1)
    if (n & 1) r = r * a % P;
  return r;
}
long long inv(long long first) {
  return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P;
}
inline int rd() {
  int first = 0;
  char p = getchar();
  while (p < '0' || p > '9') p = getchar();
  while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar();
  return first;
}
const int N = 555;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {-1, 1, 0, 0};
int n, a[N][N], vis[N * N], d[N * N], c[N * N], clk;
int ans[N][N];
pair<int, int> pos[N * N];
deque<int> q;
int main() {
  scanf("%d", &n);
  int now = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      a[i][j] = ++now;
      pos[now] = pair<int, int>(i, j);
      c[now] = 1;
      int u = min(i - 1, j - 1);
      int v = min(n - i, n - j);
      ans[i][j] = min(u, v);
    }
  int tot = 0;
  for (int t = 1; t <= n * n; ++t) {
    q.clear();
    int s;
    scanf("%d", &s);
    int first = pos[s].first, second = pos[s].second;
    c[s] = 0;
    if (!ans[first][second]) continue;
    q.push_back(s);
    vis[s] = ++clk, d[s] = 0;
    while (q.size()) {
      int u(q.back());
      q.pop_back();
      if (!d[u] && !ans[pos[u].first][pos[u].second] || pos[u].first == 1 ||
          pos[u].first == n || pos[u].second == 1 || pos[u].second == n) {
        tot += d[u];
        if (!d[u]) {
          for (int k = 0; k <= 3; ++k) {
            int xx = first + dx[k], yy = second + dy[k];
            ans[xx][yy] = 0;
          }
        }
        break;
      }
      for (int k = 0; k <= 3; ++k) {
        int first = pos[u].first + dx[k], second = pos[u].second + dy[k];
        if (1 <= first && first <= n && 1 <= second && second <= n &&
            vis[a[first][second]] != clk) {
          d[a[first][second]] = d[u] + c[a[first][second]];
          vis[a[first][second]] = clk;
          if (c[a[first][second]])
            q.push_front(a[first][second]);
          else
            q.push_back(a[first][second]);
        }
      }
    }
  }
  printf("%d\n", tot);
}
