#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500;
const int INF = 987654321;
int N, A[MAXN + 10][MAXN + 10], P[MAXN * MAXN + 10];
int U[MAXN + 10][MAXN + 10], D[MAXN + 10][MAXN + 10], L[MAXN + 10][MAXN + 10],
    R[MAXN + 10][MAXN + 10];
long long ans;
int main() {
  int i, j, k;
  scanf("%d", &N);
  for (i = 1; i <= N * N; i++) scanf("%d", &P[i]);
  for (i = 1; i <= N; i++)
    for (j = 1; j <= N; j++) A[i][j] = min({i - 1, N - i, j - 1, N - j}) + 1;
  for (i = 0; i <= N + 1; i++)
    for (j = 0; j <= N + 1; j++) {
      U[i][j] = max(0, i - 1);
      D[i][j] = min(N + 1, i + 1);
      L[i][j] = max(0, j - 1);
      R[i][j] = min(j + 1, N + 1);
    }
  for (i = 1; i <= N * N; i++) {
    int y = (P[i] - 1) / N + 1, x = (P[i] - 1) % N + 1;
    ans += A[y][x] - 1;
    D[U[y][x]][x] = D[y][x];
    U[D[y][x]][x] = U[y][x];
    L[y][R[y][x]] = L[y][x];
    R[y][L[y][x]] = R[y][x];
    queue<pair<int, int> > Q;
    if (A[U[y][x]][x] + 1 < A[D[y][x]][x])
      Q.push({D[y][x], x}), A[D[y][x]][x] = A[U[y][x]][x] + 1;
    if (A[D[y][x]][x] + 1 < A[U[y][x]][x])
      Q.push({U[y][x], x}), A[U[y][x]][x] = A[D[y][x]][x] + 1;
    if (A[y][L[y][x]] + 1 < A[y][R[y][x]])
      Q.push({y, R[y][x]}), A[y][R[y][x]] = A[y][L[y][x]] + 1;
    if (A[y][R[y][x]] + 1 < A[y][L[y][x]])
      Q.push({y, L[y][x]}), A[y][L[y][x]] = A[y][R[y][x]] + 1;
    while (!Q.empty()) {
      int y = Q.front().first, x = Q.front().second;
      Q.pop();
      if (A[y][x] + 1 < A[D[y][x]][x])
        Q.push({D[y][x], x}), A[D[y][x]][x] = A[y][x] + 1;
      if (A[y][x] + 1 < A[U[y][x]][x])
        Q.push({U[y][x], x}), A[U[y][x]][x] = A[y][x] + 1;
      if (A[y][x] + 1 < A[y][R[y][x]])
        Q.push({y, R[y][x]}), A[y][R[y][x]] = A[y][x] + 1;
      if (A[y][x] + 1 < A[y][L[y][x]])
        Q.push({y, L[y][x]}), A[y][L[y][x]] = A[y][x] + 1;
    }
    A[y][x] = -1;
  }
  printf("%lld\n", ans);
}
