#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int a[502][502];
int p[250000];
int main() {
  int n, ans = 0, i, j;
  queue<pair<int, int>> q;
  scanf("%d", &n);
  for (i = 0; i < n * n; i++) scanf("%d", &p[i]);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      a[i][j] = min({i - 1, j - 1, n - i, n - j});
    }
  }
  for (i = 0; i < n * n; i++) {
    int x, y;
    p[i]--;
    x = p[i] / n + 1;
    y = p[i] % n + 1;
    ans += a[x][y];
    a[x][y]--;
    q.push(make_pair(x, y));
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      for (j = 0; j < 4; j++) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        if (a[nx][ny] == a[x][y] + 2) {
          a[nx][ny] = a[x][y] + 1;
          q.push(make_pair(nx, ny));
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
