#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 + 2, vector<long long>(N + 2));
  vector<vector<long long> > down(N + 2, vector<long long>(N + 2));
  vector<vector<long long> > left(N + 2, vector<long long>(N + 2));
  vector<vector<long long> > right(N + 2, vector<long long>(N + 2));
  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;
    long long tmp = 1000000000;
    long long tmpu = 1000000000, tmpd = 1000000000;
    long long tmpl = 1000000000, tmpr = 1000000000;
    tmp = 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 < tate) {
        tmpu = min(up[tate][yoko] - up[j][yoko] + left[j][yoko],
                   up[tate][yoko] - up[j][yoko] + right[j][yoko]);
      } else {
        tmpd = min(down[tate][yoko] - down[j][yoko] + left[j][yoko],
                   down[tate][yoko] - down[j][yoko] + right[j][yoko]);
      }
    }
    for (long long j = 0; j < N; j++) {
      if (j < yoko) {
        tmpl = min(left[tate][yoko] - left[j][yoko] + up[j][yoko],
                   left[tate][yoko] - left[j][yoko] + down[j][yoko]);
      } else {
        tmpd = min(right[tate][yoko] - right[j][yoko] + up[j][yoko],
                   right[tate][yoko] - right[j][yoko] + down[i][yoko]);
      }
    }
    cout << i << " " << tmpu << " " << tmpd << " " << tmpl << " " << tmpr
         << endl;
    tmp = min(min(tmpu, tmpd), min(tmpl, tmpr));
    ans += tmp;
    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;
}
