#include <bits/stdc++.h>
using namespace std;
const int maxn = 25010;
int n, a[maxn];
int d[505][505], vis[505][505], ret;
int dk[] = {-1, 0, 1, 0};
int ck[] = {0, 1, 0, -1};
void bfs(int u, int v) {
  int tmp = d[u][v] + vis[u][v];
  for (int t = 0; t <= 3; ++t) {
    int x = u + dk[t], y = v + ck[t];
    if (d[x][y] > tmp) {
      d[x][y] = tmp;
      bfs(x, y);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n * n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      d[i][j] = min(min(i, n - i + 1), min(j, n - j + 1)) - 1;
      vis[i][j] = 1;
    }
  for (int i = 1; i <= n * n; ++i) {
    int u = (a[i] + n - 1) / n;
    int v = a[i] % n;
    if (v == 0) v = n;
    vis[u][v] = 0;
    ret += d[u][v];
    bfs(u, v);
  }
  cout << ret;
}
