#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1e7;
const int dr[4] = {0, 0, 1, -1};
const int dc[4] = {1, -1, 0, 0};
void dijkstra(bool mat[500][500], int n, vector<int> &bd, vector<int> &bp,
              vector<int> &par, int s) {
  vector<int> d(n * n, INF);
  par.assign(n * n, -1);
  set<pair<int, int> > pq;
  d[s] = 0;
  pq.insert({0, s});
  int v, i, j, ii, jj, to;
  while (!pq.empty()) {
    v = pq.begin()->second;
    pq.erase(pq.begin());
    i = v / n;
    j = v % n;
    for (int dir = 0; dir < 4; dir++) {
      ii = i + dr[dir];
      jj = j + dc[dir];
      to = n * ii + jj;
      if (ii <= -1 || ii >= n || jj <= -1 || jj >= n) {
        bp.push_back(v);
        bd.push_back(d[v]);
        continue;
      }
      if (d[v] < INF && d[to] > d[v] + (mat[ii][jj] ? 1 : 0)) {
        pq.erase({d[to], to});
        d[to] = d[v] + (mat[ii][jj] ? 1 : 0);
        par[to] = v;
        pq.insert({d[to], to});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  bool mat[500][500];
  for (int i = 0; i < n; i++) memset(mat[i], true, sizeof mat[i]);
  int p[n * n];
  for (int i = 0; i < n * n; i++) cin >> p[i];
  long long count = 0;
  for (int i = 0; i < n * n; i++) {
    vector<int> bd, bp, par;
    dijkstra(mat, n, bd, bp, par, p[i] - 1);
    int at = bp[min_element(bd.begin(), bd.end()) - bd.begin()];
    for (; at != p[i] - 1; at = par[at]) {
      if (mat[at / n][at % n]) count++;
    }
    mat[(p[i] - 1) / n][(p[i] - 1) % n] = false;
  }
  cout << count << "\n";
  return 0;
}
