#include <bits/stdc++.h>
using namespace std;
using LL = long long;
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n * n);
  for (auto &x : a) cin >> x;
  vector<vector<int>> b(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      b[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
    }
  }
  int dx[]{1, -1, 0, 0};
  int dy[]{0, 0, 1, -1};
  function<void(int, int, int, int, int, int)> dfs = [&](int x, int y, int v,
                                                         int f, int t, int ff) {
    b[x][y] = v;
    for (int i = 0; i < 4; i++) {
      int xx = x + dx[i], yy = y + dy[i];
      if (xx < 0 || yy < 0 || xx >= n || yy >= n) {
        continue;
      }
      if (b[xx][yy] == f) continue;
      if (b[xx][yy] > t && ff)
        dfs(xx, yy, b[xx][yy] - 1, f, b[xx][yy] - 1, 0);
      else if (b[xx][yy] > t + 1)
        dfs(xx, yy, b[xx][yy] - 1, f, b[xx][yy] - 1, 0);
      else if (b[xx][yy] < 0) {
        dfs(xx, yy, f, f, t, ff);
      }
    }
  };
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int x = a[i] - 1;
    int t = b[x / n][x % n];
    ans += t;
    dfs(x / n, x % n, -i - 1, -i - 1, t, 1);
  }
  cout << ans << endl;
  return 0;
}
