#include <bits/stdc++.h>
using namespace std;
int n;
int ar[502][502];
bool dead[502][502];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void dfs(int y, int x) {
  --ar[y][x];
  for (int i = 0; i < 4; ++i) {
    int y1 = y + dy[i];
    int x1 = x + dx[i];
    if (ar[y1][x1] > ar[y][x] + 1 ||
        (dead[y][x] && dead[y1][x1] && ar[y1][x1] > ar[y][x]))
      dfs(y1, x1);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n + 2; ++i)
    ar[n + 1][i] = ar[i][n + 1] = ar[0][i] = ar[i][0] = -10;
  for (int i = 1; i < n + 1; ++i)
    for (int j = 1; j < n + 1; ++j)
      ar[i][j] = min({i - 1, j - 1, n - i, n - j});
  long long res = 0;
  for (int i = 0; i < n * n; ++i) {
    int viewer;
    cin >> viewer;
    --viewer;
    int y = viewer / n + 1;
    int x = viewer % n + 1;
    res += ar[y][x];
    dead[y][x] = true;
    dfs(y, x);
  }
  cout << res << endl;
}
