#include <bits/stdc++.h>
using namespace std;
int a[505][505];
int s[505][505];
int s1[250010];
long long inf = 1000000;
int n, k = 1;
int next1[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
struct node {
  int a;
  int b;
  long long cnt;
} p;
long long bfs(int x, int y) {
  queue<node> que;
  a[x][y] = 0;
  que.push((node){x, y, 0});
  int min1 = 100000000;
  while (!que.empty()) {
    node g = que.front();
    int xx = g.a;
    int yy = g.b;
    int cnt1 = g.cnt;
    que.pop();
    if (cnt1 > a[xx][yy]) {
      continue;
    }
    if (xx == 0 || yy == 0 || xx > n || yy > n) {
      min1 = min(min1, cnt1);
      if (min1 == 0) {
        break;
      }
    }
    for (int i = 0; i < 4; i++) {
      int xx1 = xx + next1[i][0];
      int yy1 = yy + next1[i][1];
      int cntt = cnt1;
      if (s[xx1][yy1] != 0) {
        cntt++;
      }
      if (a[xx1][yy1] > cntt) {
        a[xx1][yy1] = cntt;
        que.push((node){xx1, yy1, cntt});
      }
    }
  }
  return min1;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      s[i][j] = k++;
    }
  }
  for (int i = 1; i <= n * n; i++) {
    cin >> s1[i];
  }
  long long cnt = 0;
  for (int i = 1; i <= n * n; i++) {
    int x = s1[i] / (n) + 1;
    int y = s1[i] % (n);
    if (y == 0) {
      y = n;
      x--;
    }
    s[x][y] = 0;
    for (int i1 = 0; i1 <= n + 1; i1++) {
      for (int j1 = 0; j1 <= n + 1; j1++) {
        a[i1][j1] = inf;
      }
    }
    cnt += bfs(x, y);
  }
  cout << cnt << endl;
  return 0;
}
