#include <bits/stdc++.h>
using namespace std;
const int MAX = 510;
const int MOD = 1000000007;
int main() {
  long long N;
  cin >> N;
  vector<long long> a(N * N);
  for (long long i = 0; i < N * N; i++) cin >> a[i];
  for (long long i = 0; i < N * N; i++) a[i]--;
  long long ans = 0;
  vector<vector<long long> > up(N, vector<long long>(N));
  vector<vector<long long> > down(N, vector<long long>(N));
  vector<vector<long long> > left(N, vector<long long>(N));
  vector<vector<long long> > right(N, vector<long long>(N));
  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < N; j++) {
      up[i][j] = i;
      down[i][j] = N - i - 1;
      left[i][j] = j;
      right[i][j] = N - j - 1;
    }
  }
  for (long long i = 0; i < N * N; i++) {
    long long tate = a[i] / N;
    long long yoko = a[i] % N;
    ans += min(min(up[tate][yoko], down[tate][yoko]),
               min(left[tate][yoko], right[tate][yoko]));
    for (long long j = 0; j < N; j++) {
      if (j < yoko) {
        right[tate][j]--;
        if (right[tate][j] < 0) right[tate][j] = 0;
      } else {
        left[tate][j]--;
        if (left[tate][j] < 0) left[tate][j] = 0;
      }
    }
    for (long long j = 0; j < N; j++) {
      if (j < tate) {
        down[j][yoko]--;
        if (down[j][yoko] < 0) down[j][yoko] = 0;
      } else {
        up[j][yoko]--;
        if (up[j][yoko] < 0) up[j][yoko] = 0;
      }
    }
  }
  cout << ans << endl;
}
