#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long double PI = 3.14159265358979;
const long long MAX = 0;
long long x[4] = {1, -1, 0, 0};
long long y[4] = {0, 0, 1, -1};
int main() {
  long long N, ans = 0;
  cin >> N;
  bool seet[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      seet[i][j] = true;
    }
  }
  long long a[N * N];
  for (int i = 0; i < N * N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < N * N; i++) {
    long long per = a[i];
    per--;
    pair<long long, long long> p = {per / N, per % N};
    seet[p.first][p.second] = false;
    long long num[N][N];
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        num[i][j] = MOD;
      }
    }
    num[p.first][p.second] = 0;
    queue<pair<long long, long long>> q;
    q.push(p);
    while (!q.empty()) {
      pair<long long, long long> pa = q.front();
      q.pop();
      for (int i = 0; i < 4; i++) {
        if (pa.first + x[i] < 0 || pa.first + x[i] >= N ||
            pa.second + y[i] < 0 || pa.second + y[i] >= N) {
          continue;
        }
        if (num[pa.first + x[i]][pa.second + y[i]] <=
            num[pa.first][pa.second]) {
          continue;
        } else {
          if (seet[pa.first + x[i]][pa.second + y[i]]) {
            num[pa.first + x[i]][pa.second + y[i]] =
                num[pa.first][pa.second] + 1;
          } else {
            num[pa.first + x[i]][pa.second + y[i]] = num[pa.first][pa.second];
          }
          q.push({pa.first + x[i], pa.second + y[i]});
        }
      }
    }
    long long cnt = MOD;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i * j == 0 || i == N - 1 || j == N - 1) {
          cnt = min(cnt, num[i][j]);
        }
      }
    }
    if (cnt > 0) {
      ans += cnt;
    }
  }
  cout << ans;
}
