#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 61;
const long long MOD = 1000000007;
const long long MAX_N = 500010;
long long a, b, c, d, e, f, h, x, y, z, p, q, m, n, t, r, k, w, l, ans, i, j;
string S, T;
vector<long long> A, B;
vector<long long> g[MAX_N];
vector<long long> visited(MAX_N, 0), used(MAX_N, 0);
vector<long long> D(MAX_N);
void solve() {
  for (long long i = 0; i < long long(n); i++) {
    for (long long j = 0; j < long long(n - 1); j++) {
      g[i * n + j].push_back(i * n + j + 1);
      g[i * n + j + 1].push_back(i * n + j);
      g[(j + 1) * n + i].push_back(j * n + i);
      g[j * n + i].push_back((j + 1) * n + i);
    }
  }
  for (long long i = 0; i < long long(n); i++) {
    for (long long j = 0; j < long long(n); j++) {
      D[i * n + j] = min({i, n - 1 - i, j, n - 1 - j});
    }
  }
  for (long long i = 0; i < long long(n * n); i++) {
    x = A[i];
    used[x] = 1;
    p = 0;
    queue<long long> Q;
    Q.push(x);
    vector<long long> K;
    visited[x] = 1;
    K.push_back(x);
    while (!Q.empty()) {
      int v = Q.front();
      Q.pop();
      for (long long x : g[v]) {
        if (visited[x] == 0 && D[x] > D[v] + 1 && used[v] == 0) {
          visited[x] = 1;
          K.push_back(x);
          Q.push(x);
          D[x] = D[v] + 1;
        }
        if (visited[x] == 0 && D[x] > D[v] && used[v] == 1) {
          visited[x] = 1;
          Q.push(x);
          K.push_back(x);
          D[x] = D[v];
        }
      }
    }
    for (long long i : K) visited[i]--;
    ans += D[x];
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  for (long long i = 0; i < long long(n * n); i++) {
    cin >> a;
    a--;
    A.push_back(a);
  }
  solve();
}
