#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int MAXN = 505;
const int MAXV = 250005;
const int MOD = 1000000007;
int P[MAXV], N, V, U[MAXV], D[MAXV], L[MAXV], R[MAXV];
bool active[MAXV];
vector<pair<int, int> > adjList[MAXV];
int dx[] = {1, -1, N, -N};
int dist[MAXV];
vector<int> vis;
int bfs(int S) {
  for (auto i : vis) dist[i] = -1;
  vis.clear();
  queue<int> q;
  dist[S] = 0;
  q.push(S);
  vis.push_back(S);
  while (q.size()) {
    int node = q.front();
    q.pop();
    if (node == V + 1) return dist[node];
    if (dist[L[node]] == -1)
      q.push(L[node]), dist[L[node]] = dist[node] + 1, vis.push_back(L[node]);
    if (dist[R[node]] == -1)
      q.push(R[node]), dist[R[node]] = dist[node] + 1, vis.push_back(R[node]);
    if (dist[U[node]] == -1)
      q.push(U[node]), dist[U[node]] = dist[node] + 1, vis.push_back(U[node]);
    if (dist[D[node]] == -1)
      q.push(D[node]), dist[D[node]] = dist[node] + 1, vis.push_back(D[node]);
  }
}
int main() {
  memset(dist, -1, sizeof dist);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  N = 500;
  cin >> N;
  for (int i = (1); i <= (V); i++) cin >> P[i];
  for (int i = (1); i <= (V); i++) {
    if (i <= N)
      U[i] = V + 1;
    else
      U[i] = i - N;
    if (i > N * N - N + 1)
      D[i] = V + 1;
    else
      D[i] = i + N;
    if (i % N == 1)
      L[i] = V + 1;
    else
      L[i] = i - 1;
    if (i % N == 0)
      R[i] = V + 1;
    else
      R[i] = i + 1;
  }
  int ans = 0;
  for (int i = (1); i <= (V); i++) {
    ans += bfs(P[i]) - 1;
    active[P[i]] = 0;
    L[R[P[i]]] = L[P[i]];
    R[L[P[i]]] = R[P[i]];
    U[D[P[i]]] = U[P[i]];
    D[U[P[i]]] = D[P[i]];
  }
  cout << ans;
}
