#include <bits/stdc++.h>
using namespace std;
int N;
vector<int> P(505);
bool a[505][505];
int d[505][505];
void dfs(int i, int j, int dst) {
  if (dst >= d[i][j]) return;
  d[i][j] = dst;
  if (i > 0) dfs(i - 1, j, dst + (1 - a[i - 1][j]));
  if (i < N - 1) dfs(i + 1, j, dst + (1 - a[i + 1][j]));
  if (j > 0) dfs(i, j - 1, dst + (1 - a[i][j - 1]));
  if (j < N - 1) dfs(i, j + 1, dst + (1 - a[i][j + 1]));
}
void print() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cout << d[i][j] << " ";
    }
    cout << "\n";
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N * N; i++) cin >> P[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      d[i][j] = min(min(i, N - i - 1), min(j, N - j - 1)) + 1;
    }
  }
  int s = 0;
  for (int r = 0; r < N * N; r++) {
    int i = (P[r] - 1) / N;
    int j = (P[r] - 1) % N;
    a[i][j] = true;
    int dst = d[i][j] - 1;
    if (i > 0) dst = min(dst, d[i - 1][j]);
    if (i < N - 1) dst = min(dst, d[i + 1][j]);
    if (j > 0) dst = min(dst, d[i][j - 1]);
    if (j < N - 1) dst = min(dst, d[i][j + 1]);
    s += dst;
    dfs(i, j, dst);
  }
  cout << s << "\n";
  return 0;
}
