#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, INF = mod * mod * 3LL;
vector<vector<int> > w, d;
int n, dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
void dfs(int y, int x) {
  for (int k = 0; k < 4; k++) {
    int i = y + dy[k];
    int j = x + dx[k];
    if (i < 0 || i >= n || j < 0 || j >= n) continue;
    if (d[i][j] > d[y][x] + w[i][j]) d[i][j] = d[y][x] + w[i][j], dfs(i, j);
  }
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int ans = 0, p;
  cin >> n;
  w.resize(n, vector<int>(n, 1));
  d.resize(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = min(min(i, j), min(n - 1 - i, n - j - 1));
    }
  }
  for (int i = 0; i < n * n; i++) {
    cin >> p;
    p--;
    int x = p % n;
    int y = p / n;
    ans += d[y][x];
    w[y][x] = 0;
    dfs(y, x);
  }
  cout << ans << endl;
  return 0;
}
