#include <bits/stdc++.h>
using namespace std;
const int N = 505;
bool f[N][N];
int n, ans, vis[N][N], cnt;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
deque<int> q;
int main() {
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cnt++;
    int x;
    cin >> x;
    int a = x / n + 1 - (x % n == 0), b = x % n;
    if (b == 0) b = n;
    q.push_back(a);
    q.push_back(b);
    q.push_back(0);
    f[a][b] = 1;
    while (!q.empty()) {
      a = q.front();
      q.pop_front();
      b = q.front();
      q.pop_front();
      int move = q.front();
      q.pop_front();
      bool flag = false;
      vis[a][b] = cnt;
      for (int i = 0; i < 4; i++) {
        int A = a + dx[i];
        int B = b + dy[i];
        if (vis[A][B] == cnt) continue;
        if ((A >= 1 && B >= 1 && A <= n && B <= n)) {
          vis[A][B] = cnt;
          if (f[A][B]) {
            q.push_front(move);
            q.push_front(B);
            q.push_front(A);
          } else {
            q.push_back(A);
            q.push_back(B);
            q.push_back(move + 1);
          }
        } else {
          flag = true;
          ans += move;
          break;
        }
      }
      if (flag) break;
    }
    q.clear();
  }
  cout << ans << "\n";
  return 0;
}
