#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, a[250010], dist[510][510], rem[510][510], ans = 0, dx[] = {1, -1, 0, 0},
                                                 dy[] = {0, 0, 1, -1};
bool out(int x, int y) { return x < 1 || x > n || y < 1 || y > n; }
bool upd(int x, int y) {
  int sv = dist[x][y];
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (out(xx, yy)) continue;
    dist[x][y] = min(dist[x][y], dist[xx][yy] + rem[xx][yy]);
  }
  return dist[x][y] != sv;
}
void dfs(int x, int y) {
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (out(xx, yy) || !upd(xx, yy)) continue;
    dfs(xx, yy);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n * n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      rem[i][j] = 1;
      dist[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
    }
  for (int i = 0; i < n * n; i++) {
    int x = (a[i] - 1) / n + 1, y = a[i] % n;
    ans += dist[x][y];
    rem[x][y] = 0;
    dfs(x, y);
  }
  cout << ans << endl;
  return 0;
}
