#include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int> > M;
vector<vector<int> > F;
void printMtx() {
  for (int i = 0; i <= N + 1; i++) {
    for (int j = 0; j <= N + 1; j++) {
      if (M[i][j] == -1) {
        cout << "* ";
      } else {
        cout << M[i][j] << " ";
      }
    }
    cout << endl;
  }
  for (int i = 0; i <= N + 1; i++) {
    for (int j = 0; j <= N + 1; j++) {
      cout << F[i][j] << " ";
    }
    cout << endl;
  }
}
vector<int> iT = {1, 0, -1, 0};
vector<int> jT = {0, 1, 0, -1};
void update(int i, int j) {
  int m = M[i][j];
  int f = F[i][j];
  for (int sw = 0; sw <= 1; sw++) {
    for (int k = 0; k < 4; k++) {
      int it = i + iT[k];
      int jt = j + jT[k];
      if (F[it][jt] == sw) {
        if (M[it][jt] > m + f + sw) {
          M[it][jt] = m + f + sw;
          update(it, jt);
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  M = vector<vector<int> >(N + 2, vector<int>(N + 2, -1));
  F = vector<vector<int> >(N + 2, vector<int>(N + 2, 0));
  for (int k = 0; k <= (N + 1) / 2; k++) {
    for (int i = k + 1; i <= N - k; i++) {
      for (int j = k + 1; j <= N - k; j++) {
        M[i][j] = k;
        F[i][j] = 1;
      }
    }
  }
  int cost = 0;
  for (int p = 1; p <= (N * N); p++) {
    int P;
    cin >> P;
    int j = ((P - 1) % N) + 1;
    int i = ((P - 1) / N) + 1;
    cost += M[i][j];
    M[i][j]--;
    F[i][j] = 0;
    update(i, j);
  }
  cout << cost << endl;
  return (0);
}
