#include <bits/stdc++.h>
using namespace std;
using P = pair<long long, long long>;
using Graph = vector<vector<long long>>;
using ll = long long;
signed main() {
  long long n;
  cin >> n;
  long long n2 = pow(n, 2);
  long long ex[n2];
  for (long long i = 0; i < n2; i++) {
    long long pi;
    cin >> pi;
    ex[i] = pi - 1;
  }
  vector<vector<long long>> grid(n, vector<long long>(n, 0));
  vector<vector<long long>> exis(n, vector<long long>(n, 1));
  for (long long i = 0; i < n; i++) {
    if (i == 0) continue;
    if (i == n - 1) continue;
    for (long long j = 0; j < n; j++) {
      if (j == 0 || j == n - 1) continue;
      grid[i][j] = min(min(i, n - (i + 1)), min(j, n - (j + 1)));
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n2; i++) {
    long long exp = ex[i];
    long long ni = exp / n;
    long long nj = exp - ni * n;
    exis[ni][nj] = 0;
    long long m = grid[ni][nj];
    if (ni - 1 >= 0) {
      m = min(m, grid[ni - 1][nj] + exis[ni - 1][nj]);
    }
    if (ni + 1 < n) {
      m = min(m, grid[ni + 1][nj] + exis[ni + 1][nj]);
    }
    if (nj - 1 >= 0) {
      m = min(m, grid[ni][nj - 1] + exis[ni][nj - 1]);
    }
    if (nj + 1 < n) {
      m = min(m, grid[ni][nj + 1] + exis[ni][nj + 1]);
    }
    ans += m;
    if (ni - 1 >= 0) {
      if (grid[ni - 1][nj] != 0) {
        grid[ni - 1][nj] = min(grid[ni - 1][nj], m);
      }
    }
    if (ni + 1 < n) {
      if (grid[ni + 1][nj] != 0) {
        grid[ni + 1][nj] = min(grid[ni + 1][nj], m);
      }
    }
    if (nj - 1 >= 0) {
      if (grid[ni][nj - 1] != 0) {
        grid[ni][nj - 1] = min(grid[ni][nj - 1], m);
      }
    }
    if (nj + 1 < n) {
      if (grid[ni][nj + 1] != 0) {
        grid[ni][nj + 1] = min(grid[ni][nj + 1], m);
      }
    }
  }
  cout << ans << endl;
}
