#include <bits/stdc++.h>
using namespace std;
int N;
int seat[500 * 500 + 1];
int used[500 * 500 + 1];
int move_vec[4];
typedef pair<int, int> P;
int bfs(int x) {
  cerr << "start seach:" << x << endl;
  priority_queue<P, vector<P>, greater<P> > que;
  que.push(make_pair(0, x));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int s = p.second;
    cerr << p.first << " " << p.second << endl;
    used[s] = 1;
    if ((1 <= s && s <= N) || (N * N - N + 1 <= s && s <= N * N) ||
        (s % N == 0) || ((s - 1) % N == 0)) {
      return p.first;
    }
    for (int i = 0; i < 4; i++) {
      int next_pos = s + move_vec[i];
      int next_cost = p.first + ((seat[next_pos] == 0) ? 1 : 0);
      if (used[next_pos] == 0) {
        que.push(make_pair(next_cost, next_pos));
      }
    }
  }
}
int main() {
  cin >> N;
  move_vec[0] = 1;
  move_vec[1] = -1;
  move_vec[2] = N;
  move_vec[3] = -N;
  int total_seat = 0;
  for (int k = 0; k < N * N; k++) {
    int p;
    cin >> p;
    for (int i = 0; i < 500 * 500 + 1; i++) {
      used[i] = 0;
    }
    total_seat += bfs(p);
    seat[p] = 1;
  }
  cout << total_seat << endl;
  return 0;
}
