#include <bits/stdc++.h>
const int maxn = 503;
const int dxy[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int n, p[maxn * maxn], d[maxn * maxn], o[maxn * maxn];
long long ans;
std::queue<int> q;
bool vis[maxn * maxn];
void dfs(int s) {
  int so = o[s];
  for (int i = 1; i <= n * n; ++i) {
    vis[i] = false;
  }
  q.push(s);
  vis[s] = true;
  d[s] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < 4; ++i) {
      int x = (u - 1) / n, y = (u - 1) % n;
      x += dxy[i][0];
      y += dxy[i][1];
      if (x < 0 || x >= n || y < 0 || y >= n) continue;
      int v = x * n + y + 1;
      if (vis[v]) continue;
      q.push(v);
      vis[v] = true;
      d[v] = d[u] + (o[v] < so ? 0 : 1);
    }
  }
  int md = n * 8;
  for (int i = 0; i < n; ++i) {
    md = std::min(
        {md, d[i * n + 1], d[i * n + n], d[i + 1], d[n * (n - 1) + i + 1]});
  }
  ans += md;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; ++i) {
    scanf("%d", p + i);
    o[p[i]] = i;
  }
  for (int i = 1; i <= n * n; ++i) {
    dfs(p[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
