#include <bits/stdc++.h>
using namespace std;
int n;
int ar[500][500];
bool dead[500][500];
vector<pair<int, int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
void dfs(int y, int x) {
  --ar[y][x];
  for (auto d : dirs)
    if (y + d.first < n && y + d.first >= 0 && x + d.second < n &&
        x + d.second >= 0 &&
        (ar[y + d.first][x + d.second] > ar[y][x] + 1 ||
         (dead[y][x] && dead[y + d.first][x + d.second] &&
          ar[y + d.first][x + d.second] != ar[y][x])))
      dfs(y + d.first, x + d.second);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) ar[i][j] = min({i, j, n - i - 1, n - j - 1});
  long long res = 0;
  for (int i = 0; i < n * n; ++i) {
    int viewer;
    cin >> viewer;
    --viewer;
    res += ar[viewer / n][viewer % n];
    dead[viewer / n][viewer % n] = true;
    dfs(viewer / n, viewer % n);
  }
  cout << res << endl;
}
