#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], fa[N * N], ans[N * N], vis[N][N];
int tim[N * N], clk, d[N * N];
pair<int, int> pos[N * N];
deque<int> q;
int Find(int first) { return fa[first] ? fa[first] = Find(fa[first]) : first; }
void U(int first, int second) {
  first = Find(first), second = Find(second);
  if (first != second) {
    if (ans[first] < ans[second])
      fa[second] = first;
    else
      fa[first] = second;
  }
}
void chkmin(int &a, int b) { a = min(a, b); }
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);
      int u = min(n - i, n - j);
      int v = min(i - 1, j - 1);
      ans[now] = min(u, v);
      vis[i][j] = 1;
    }
  for (int i = 1; i <= n; ++i)
    U(a[0][i] = ++now, 0), U(a[i][0] = ++now, 0), U(a[n + 1][i] = ++now, 0),
        U(a[i][n + 1] = ++now, 0);
  int tot = 0;
  for (int i = 1; i <= n * n; ++i) {
    int t;
    scanf("%d", &t);
    int first = pos[t].first, second = pos[t].second;
    for (int k = 0; k <= 3; ++k) {
      int xx = first + dx[k], yy = second + dy[k];
      if (1 <= xx && xx <= n && 1 <= yy && yy <= n) {
        if (!vis[xx][yy]) U(a[first][second], a[xx][yy]);
      }
    }
    for (int k = 0; k <= 3; ++k) {
      int xx = first + dx[k], yy = second + dy[k];
      if (1 <= xx && xx <= n && 1 <= yy && yy <= n) {
        if (vis[xx][yy]) chkmin(ans[Find(t)], ans[a[xx][yy]] + 1);
      }
    }
    vis[first][second] = 0;
    if (!ans[Find(t)]) continue;
    q.clear();
    q.push_back(t);
    tim[t] = ++clk, d[t] = 0;
    while (q.size()) {
      int u(q.back());
      q.pop_back();
      if (pos[u].first == 1 || pos[u].first == n || pos[u].second == 1 ||
          pos[u].second == n) {
        tot += d[u];
        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 &&
            tim[a[first][second]] != clk) {
          d[a[first][second]] = d[u] + vis[first][second];
          tim[a[first][second]] = clk;
          if (vis[first][second])
            q.push_front(a[first][second]);
          else
            q.push_back(a[first][second]);
        }
      }
    }
  }
  printf("%d\n", tot);
}
