#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const int inf = 1 << 30;
const int maxn = 3e5 + 5;
inline void fastio() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
int n;
long long f[505][505], ans;
int dx[5] = {-1, 1, 0, 0};
int dy[5] = {0, 0, -1, 1};
void dfs(int x, int y) {
  for (int i = (0); i <= (3); i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 1 && nx <= n && ny >= 1 && ny <= n) {
      if (f[nx][ny] == f[x][y] + 1) {
        dfs(nx, ny);
      }
    }
  }
  f[x][y]--;
}
int main() {
  fastio();
  cin >> n;
  int x, y, s;
  for (int i = (1); i <= (n); i++)
    for (int j = (1); j <= (n); j++) {
      f[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
    }
  ans = 0;
  for (int i = (1); i <= (n * n); i++) {
    cin >> s;
    x = (s + n - 1) / n;
    y = s - (x - 1) * n;
    ans += f[x][y];
    dfs(x, y);
    f[x][y] = 0;
  }
  cout << ans << endl;
  return 0;
}
