#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 500;
int checked[MAX_N][MAX_N];
void dfs(int N, int y, int x, vector<vector<int>> &v, vector<vector<int>> has,
         int new_v) {
  v[y][x] = new_v;
  checked[y][x] = 1;
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  for (int i = 0; i < 4; i++) {
    int yy = y + dy[i];
    int xx = x + dx[i];
    if (0 <= yy && yy < N && 0 <= xx && xx < N) {
      if (!checked[yy][xx] && v[yy][xx] > new_v + has[y][x]) {
        dfs(N, yy, xx, v, has, new_v + has[y][x]);
      }
    }
  }
}
void solve(int N, vector<int> P) {
  vector<vector<int>> v(N, vector<int>(N));
  vector<vector<int>> has(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      v[i][j] = min({
          i,
          N - 1 - i,
          j,
          N - 1 - j,
      });
      has[i][j] = 1;
    }
  }
  int ans = 0;
  for (auto p : P) {
    int y = (p - 1) / N;
    int x = (p - 1) % N;
    has[y][x] = 0;
    ans += v[y][x];
    int new_v = min({
        (y > 0 ? v[y - 1][x] + has[y - 1][x] : 0),
        (y < N - 1 ? v[y + 1][x] + has[y + 1][x] : 0),
        (x > 0 ? v[y][x - 1] + has[y][x - 1] : 0),
        (x < N - 1 ? v[y][x + 1] + has[y][x + 1] : 0),
    });
    memset(checked, 0, sizeof(checked));
    dfs(N, y, x, v, has, new_v);
  }
  cout << ans << endl;
}
int main() {
  int N;
  vector<int> P;
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    P.push_back(p);
  }
  solve(N, P);
  return 0;
}
