#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n;

int main() {
  cin >> n;
  vector<int> p(n * n);
  range(i, 0, n * n) cin >> p[i];
  vector<vector<int>> res(n, vector<int>(n));
  vector<vector<int>> was(n, vector<int>(n, 1));
  range(i, 0, n) {
    range(j, 0, n) res[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
  }
  vector<int> dx = {0, 0, -1, 1};
  vector<int> dy = {1, -1, 0, 0};
  function<void(int, int)> dfs = [&](int x, int y) {
    range(rot, 0, 4) {
      int sx = x + dx[rot];
      int sy = y + dy[rot];
      if(sx < 0 || sx >= n || sy < 0 || sy >= n) continue;
      if(res[sx][sy] > res[x][y] + was[x][y]) {
        res[sx][sy] = res[x][y] + was[x][y];
        dfs(sx, sy);
      }
    }
  };
  int ans = 0;
  for(int i : p) {
    i--;
    int x = i / n;
    int y = i % n;
    ans += res[x][y];
    was[x][y] = 0;
    dfs(x, y);
  }
  cout << ans;
  return 0;
}
