#include <bits/stdc++.h>
using namespace std;
int N;
const int INF = 1000;
int ans[505][505];
bool used[505][505];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool in_field(int h, int w) { return h >= 0 && h < N && w >= 0 && w < N; }
int p[50000];
int to_h(int idx) { return idx / N; }
int to_w(int idx) { return idx % N; }
int calc_max(int h, int w) {
  int ret = INF;
  for (int i = 0; i < 4; i++) {
    if (in_field(h + dx[i], w + dy[i])) {
      ret = min(ret, ans[h + dx[i]][w + dy[i]]);
    } else {
      ret = 0;
    }
  }
  if (used[h][w]) ret++;
  return ret;
}
void dfs(int h, int w) {
  ans[h][w] = calc_max(h, w);
  for (int i = 0; i < 4; i++) {
    if (in_field(h + dx[i], w + dy[i])) {
      if (calc_max(h + dx[i], w + dy[i]) < ans[h + dx[i]][w + dy[i]])
        dfs(h + dx[i], w + dy[i]);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    int s;
    cin >> s;
    s--;
    p[i] = s;
  }
  long long ret = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      used[i][j] = true;
      ans[i][j] = min({i, j, (N - i - 1), (N - j - 1)}) + 1;
    }
  }
  for (int i = 0; i < N * N; i++) {
    int idx = p[i];
    int h = to_h(idx);
    int w = to_w(idx);
    ret += ans[h][w];
    used[h][w] = false;
    dfs(h, w);
  }
  cout << ret - N * N << endl;
}
