#include <bits/stdc++.h>
using namespace std;
int n, m, dp[250002], a[250002], ans, b[250002];
inline int read() {
  register int t = 0;
  register char v = getchar();
  while (v < '0') v = getchar();
  while (v >= '0') t = (t << 3) + (t << 1) + v - 48, v = getchar();
  return t;
}
inline void dfs(register int x, register int y, register int z = 0) {
  if (!z && y >= dp[x]) return;
  if (!b[x]) ++y;
  dp[x] = y;
  if (x > n) dfs(x - n, y);
  if (x <= n * n - n) dfs(x + n, y);
  if (x % n != 1) dfs(x - 1, y);
  if (x % n) dfs(x + 1, y);
}
int main() {
  n = read();
  for (register int i = 1; i <= n * n; ++i) a[i] = read();
  for (register int i = 1; i <= n; ++i)
    for (register int j = 1; j <= n; ++j)
      dp[(i - 1) * n + j] = min(i - 1, min(j - 1, min(n - i, n - j))) + 1;
  for (register int i = 1; i <= n * n; ++i) {
    ans += dp[a[i]] - 1;
    b[a[i]] = 1;
    dfs(a[i], dp[a[i]] - 1, 1);
  }
  printf("%d", ans);
}
