#include <bits/stdc++.h>
using namespace std;
const int INF = 2012345678;
const long long LLINF = 4012345678012345678LL;
const long long MOD = 1000000007;
const long double PI = 3.1415926535898;
const long double EPS = 1e-9;
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  return gcd(b, a % b);
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long expo(long long b, long long p, long long m) {
  long long res = 1;
  while (p) {
    if (p & 1) res = (res * b) % m;
    b = (b * b) % m;
    p >>= 1;
  }
  return res;
}
inline long long modinv(long long a, long long m) { return expo(a, m - 2, m); }
int main() {
  int n;
  scanf("%d", &n);
  int N = n * n;
  int arr[N];
  for (int i = 0; i < N; i++) {
    scanf("%d", &arr[i]);
    arr[i]--;
  }
  int grid[N];
  for (int i = 0; i < N; i++) grid[i] = 1;
  int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
  int dist[N];
  for (int i = 0; i < N; i++) dist[i] = INF;
  vector<int> added;
  deque<int> q;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    q.push_back(arr[i]);
    added.push_back(arr[i]);
    dist[arr[i]] = 0;
    while (!q.empty()) {
      int cur = q.front();
      q.pop_front();
      int x = cur / n, y = cur % n;
      if (x == 0 || y == 0 || x == n - 1 || y == n - 1) {
        ans += dist[cur];
        break;
      }
      int mini = INF;
      for (int j = 0; j < 4; j++) {
        int nx = x + dx[j], ny = y + dy[j];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          int k = nx * n + ny;
          if (dist[k] > dist[cur] + grid[k]) {
            dist[k] = dist[cur] + grid[k];
            if (grid[k] == 0)
              q.push_front(k);
            else
              q.push_back(k);
            added.push_back(k);
          }
        }
      }
    }
    q.clear();
    while (!added.empty()) {
      dist[added.back()] = INF;
      added.pop_back();
    }
    grid[arr[i]] = 0;
  }
  printf("%lld", ans);
}
