#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); }
inline void read(int& x) {
  x = 0;
  char ch = getchar_unlocked();
  while (ch < '0' || ch > '9') ch = getchar_unlocked();
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch & 15);
    ch = getchar_unlocked();
  }
}
int main() {
  int n;
  read(n);
  int N = n * n;
  int arr[N];
  for (int i = 0; i < N; i++) {
    read(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;
  int added[N];
  int sza = 0;
  int q[2 * N + 5];
  int lq = N + 2, rq = N + 2;
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    added[sza++] = arr[i];
    lq = rq = N + 2;
    q[rq++] = arr[i];
    dist[arr[i]] = 0;
    while (lq != rq) {
      int cur = q[lq++];
      int x = cur / n, y = cur % n;
      if (x == 0 || y == 0 || x == n - 1 || y == n - 1) {
        ans += dist[cur];
        break;
      }
      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[lq--] = k;
            else
              q[rq++] = k;
            added[sza++] = k;
          }
        }
      }
    }
    while (sza > 0) {
      dist[added[sza - 1]] = INF;
      sza--;
    }
    grid[arr[i]] = 0;
  }
  printf("%lld", ans);
}
