#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
int main() {
  int n;
  cin >> n;
  int p[n * n + 1];
  Graph G(n * n + 1);
  for (int i = 1; i <= n * n; i++) cin >> p[i];
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < n; j++) {
      G[j * n + i].push_back(j * n + i + 1);
      G[j * n + i + 1].push_back(j * n + i);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n - 1; j++) {
      G[j * n + i].push_back((j + 1) * n + i);
      G[(j + 1) * n + i].push_back(j * n + i);
    }
  }
  for (int i = 1; i <= n; i++) {
    G[i].push_back(0);
    G[0].push_back(i);
    G[(n - 1) * n + i].push_back(0);
    G[0].push_back((n - 1) * n + i);
  }
  for (int i = 1; i < n - 1; i++) {
    G[i * n + 1].push_back(0);
    G[0].push_back(i * n + 1);
    G[i * n + n].push_back(0);
    G[0].push_back(i * n + n);
  }
  int64_t ans = 0;
  set<int> pass;
  pass.insert(0);
  for (int i = 1; i <= n * n; i++) {
    vector<int> dist(n * n + 1, -1);
    queue<int> que;
    dist[p[i]] = 0;
    que.push(p[i]);
    while (!que.empty()) {
      int v = que.front();
      que.pop();
      for (int nv : G[v]) {
        if (dist[nv] != -1 && dist[nv] <= dist[v] + (!pass.count(nv))) continue;
        dist[nv] = dist[v] + (!pass.count(nv));
        if (v == 11 && nv == 10) cout << dist[v] << endl;
        que.push(nv);
      }
    }
    ans += dist[0];
    pass.insert(p[i]);
  }
  cout << ans << endl;
}
