#include <bits/stdc++.h>
using namespace std;
long long N, N2;
pair<long long, long long> P[1010101];
long long mk[1000][1000];
long long mk2[1000][1000];
long long gx[] = {1, 0, -1, 0};
long long gy[] = {0, 1, 0, -1};
long long i, j, k;
int main() {
  scanf("%lld", &N);
  N2 = N * N;
  for (i = 0; i < N2; ++i) {
    long long p;
    scanf("%lld", &p);
    --p;
    P[i] = {p / N, p % N};
  }
  for (i = 0; i < N; ++i) {
    for (j = 0; j < N; ++j) {
      mk[i][j] = min(min(i, N - i - 1), min(j, N - j - 1));
    }
  }
  long long ans = 0;
  for (i = 0; i < N2; ++i) {
    long long px = P[i].first;
    long long py = P[i].second;
    ans += mk[px][py];
    mk2[px][py] = 1;
    queue<pair<long long, long long> > q;
    q.push({px, py});
    while (q.size()) {
      pair<long long, long long> qa = q.front();
      q.pop();
      for (j = 0; j < 4; ++j) {
        long long x = qa.first + gx[j];
        long long y = qa.second + gy[j];
        if (x < 0 || N <= x || y < 0 || N <= y) continue;
        long long b = mk[qa.first][qa.second] + 1 - mk2[qa.first][qa.second];
        if (b < mk[y][x]) {
          mk[y][x] = b;
          q.push({y, x});
        }
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
