#include <bits/stdc++.h>
using namespace std;
long long n, m, a[510][510], dis[510][510], p[50100], ans;
void dfs(long long x, long long y) {
  if (dis[x - 1][y] > dis[x][y] + a[x][y]) {
    dis[x - 1][y] = dis[x][y] + a[x][y];
    dfs(x - 1, y);
  }
  if (dis[x][y - 1] > dis[x][y] + a[x][y]) {
    dis[x][y - 1] = dis[x][y] + a[x][y];
    dfs(x, y - 1);
  }
  if (dis[x + 1][y] > dis[x][y] + a[x][y]) {
    dis[x + 1][y] = dis[x][y] + a[x][y];
    dfs(x + 1, y);
  }
  if (dis[x][y + 1] > dis[x][y] + a[x][y]) {
    dis[x][y + 1] = dis[x][y] + a[x][y];
    dfs(x, y + 1);
  }
}
int main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n * n; i++) scanf("%lld", &p[i]);
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      a[i][j] = 1;
      dis[i][j] = min(i - 1, min(j - 1, min(n - i, n - j)));
    }
  }
  int x, y;
  for (long long i = 1; i <= n * n; i++) {
    y = p[i] % n;
    if (y == 0) y = n;
    x = (p[i] - y) / n + 1;
    ans += dis[x][y];
    a[x][y] = 0;
    dfs(x, y);
  }
  printf("%lld\n", ans);
  return 0;
}
