#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF(1 << 30);
struct edge {
  int to, cost;
};
void dijkstra(int s, vector<int> &d, vector<vector<edge>> &G) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  for (int i = 0; i < (int)(d.size()); i++) d[i] = INF;
  d[s] = 0;
  que.push(pair<int, int>(0, s));
  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first) continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pair<int, int>(d[e.to], e.to));
      }
    }
  }
}
int main() {
  int N;
  cin >> N;
  vector<int> isSit(N * N, 1);
  vector<int> idxEdge;
  for (int i = 0; i < (int)(N); i++) {
    idxEdge.push_back(i);
    idxEdge.push_back(i + N * (N - 1));
  }
  for (int i = 1; i <= (N - 2); i++) {
    idxEdge.push_back(i * N);
    idxEdge.push_back(i * N + (N - 1));
  }
  int ans = 0;
  for (int Z = 0; Z < (int)(N * N); Z++) {
    int p;
    cin >> p;
    p--;
    isSit[p] = 0;
    if ((p - 1) / N == 0) continue;
    if ((p - 1) / N == N - 1) continue;
    if (p % N == 0) continue;
    if (p % N == (N - 1)) continue;
    vector<vector<edge>> e(N * N);
    for (int i = 0; i < (int)(N * N); i++) {
      int up = i - N;
      if (up >= 0) {
        e[i].push_back((edge){up, isSit[up]});
      }
      int down = i + N;
      if (down <= N * N - 1) {
        e[i].push_back((edge){down, isSit[down]});
      }
      if (i % N != 0) {
        int left = i - 1;
        e[i].push_back((edge){left, isSit[left]});
      }
      if (i % N != (N - 1)) {
        int right = i + 1;
        e[i].push_back((edge){right, isSit[right]});
      }
    }
    vector<int> d(N * N);
    dijkstra(p, d, e);
    int tmp = INF;
    for (int x : idxEdge) tmp = min(tmp, d[x]);
    ans += tmp;
  }
  cout << ans << endl;
  return 0;
}
