#include <bits/stdc++.h>
using namespace std;
void solve(long long v, long long cost) {}
int main(void) {
  int n;
  cin >> n;
  vector<bool> p(n * n, true);
  int sum_cost = 0;
  for (long long i = 0; i < (long long)(n * n); i++) {
    int x;
    cin >> x;
    queue<pair<int, int>> q;
    q.push(make_pair(x, 0));
    int min_cost = 1000000007;
    vector<bool> visited(n * n + 1, false);
    while (!q.empty()) {
      pair<int, int> pq = q.front();
      int pos = pq.first;
      int cost = pq.second;
      q.pop();
      if (cost > n) continue;
      visited[pos] = true;
      if (pos - n < 0 || pos % n == 1 || pos % n == 0 || pos + n > n * n) {
        if (pos - n < 0 || pos % n == 1 || pos % n == 0 || pos + n > n * n) {
        } else {
        }
        min_cost = min(min_cost, cost);
      } else {
        if (!visited[pos - 1]) {
          if (p[pos - 1])
            q.push(make_pair(pos - 1, cost + 1));
          else
            q.push(make_pair(pos - 1, cost));
        }
        if (!visited[pos + 1]) {
          if (p[pos + 1])
            q.push(make_pair(pos + 1, cost + 1));
          else
            q.push(make_pair(pos + 1, cost));
        }
        if (!visited[pos - n]) {
          if (p[pos - n])
            q.push(make_pair(pos - n, cost + 1));
          else
            q.push(make_pair(pos - n, cost));
        }
        if (!visited[pos + n]) {
          if (p[pos + n])
            q.push(make_pair(pos + n, cost + 1));
          else
            q.push(make_pair(pos + n, cost));
        }
      }
    }
    sum_cost += min_cost;
    p[x] = false;
  }
  cout << sum_cost << endl;
  return 0;
}
