#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
using uint = unsigned int;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1);
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int siz = N * N;
  vector<bool> exist(siz, true);
  int ans = 0;
  for (int i = 0; i < siz; ++i) {
    int P;
    cin >> P;
    P--;
    exist[P] = false;
    bool finished = false;
    vector<int> dist(siz, INT_MAX);
    deque<int> que;
    dist[P] = 0;
    que.push_back(P);
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    while (!que.empty()) {
      int v = que.front();
      que.pop_front();
      int y = v / N, x = v % N;
      for (int j = 0; j < 4; ++j) {
        int ny = y + dy[j];
        int nx = x + dx[j];
        if (ny < 0 || nx < 0 || ny >= N || nx >= N) {
          ans += dist[v];
          finished = true;
          break;
        }
        int nv = ny * N + nx;
        if (exist[nv] && dist[nv] > dist[v] + 1)
          dist[nv] = dist[v] + 1, que.push_back(nv);
        else if (!exist[nv] && dist[nv] > dist[v])
          dist[nv] = dist[v], que.push_front(nv);
      }
      if (finished) break;
    }
  }
  cout << ans << endl;
  return 0;
}
