#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
const int INF = 1e9;
bool G[maxn][maxn], vis[maxn][maxn];
int dis[maxn][maxn];
int p[maxn * maxn], n;
void spfa(int sx, int sy) {
  queue<pair<int, int> > q;
  q.push(make_pair(sx, sy));
  dis[sx][sy] = 0;
  vis[sx][sy] = 1;
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    int ux = u.first, uy = u.second;
    vis[ux][uy] = 0;
    for (int i = -1; i <= 1; i++)
      for (int j = -1; j <= 1; j++)
        if (abs(i + j) == 1) {
          int vx = ux + i, vy = uy + j;
          if (vx < 0 or vx > n + 1 or vy < 0 or vy > n + 1) continue;
          if (dis[vx][vy] > dis[ux][uy] + (!G[vx][vy])) {
            dis[vx][vy] = dis[ux][uy] + (!G[vx][vy]);
            if (!vis[vx][vy]) {
              vis[vx][vy] = 1;
              q.push(make_pair(vx, vy));
            }
          }
        }
  }
  G[sx][sy] = 1;
}
int main() {
  cin >> n;
  long long ans = 0;
  for (int i = 0; i <= n + 1; i++)
    G[0][i] = G[n + 1][i] = G[i][0] = G[i][n + 1] = 1;
  for (int t = 1; t <= n * n; t++) {
    int x;
    cin >> x;
    int sx = (x - 1) / n + 1, sy = x % n;
    if (!sy) sy = n;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= n + 1; j++) {
        dis[i][j] = INF;
        vis[i][j] = 0;
      }
    spfa(sx, sy);
    ans += dis[0][0];
  }
  cout << ans;
  return 0;
}
