#include <bits/stdc++.h>
using namespace std;
int n, a[250005], is[505][505], v[505][505];
int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0}, ans = 0;
int dis[505][505] = {0};
struct Pair {
  int x, y;
};
int bfs(int x, int y, int g) {
  deque<Pair> q;
  q.push_back({x, y}), dis[x][y] = 0, v[x][y] = g;
  while (!q.empty()) {
    Pair now = q.front();
    q.pop_front();
    for (int i = 0; i < 4; i++) {
      Pair tmp = {now.x + dx[i], now.y + dy[i]};
      if (tmp.x < 1 || tmp.y > n || tmp.y < 1 || tmp.x > n)
        return dis[now.x][now.y];
      if (v[tmp.x][tmp.y] == g) continue;
      if (is[tmp.x][tmp.y]) {
        q.push_front(tmp);
        dis[tmp.x][tmp.y] = dis[now.x][now.y], v[tmp.x][tmp.y] = g;
      } else {
        q.push_back(tmp);
        dis[tmp.x][tmp.y] = dis[now.x][now.y] + 1, v[tmp.x][tmp.y] = g;
      }
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n * n; i++) {
    int x = (a[i] - 1) / n + 1, y = (a[i] - 1) % n + 1;
    ans += bfs(x, y, i), is[x][y] = 1;
  }
  cout << ans;
}
