#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9 + 7;
const int MAX = 3e5 + 10;
const long long LNF = 2e18;
inline int _min(int a, int b) { return a < b ? a : b; }
int n, P[MAX];
bool B[510][510];
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
bool valid(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; }
int U[MAX], D[MAX];
int find(int x) { return x == U[x] ? x : U[x] = find(U[x]); }
void unite(int x, int y) {
  x = find(x), y = find(y);
  U[y] = x;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int x = 1; x <= n; x++)
    for (int y = 1; y <= n; y++) {
      int i = (x - 1) * n + y;
      D[i] = min({x - 1, y - 1, n - x, n - y});
      U[i] = i;
      B[x][y] = true;
    }
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int p;
    cin >> p;
    P[i] = p;
    int x = (p + n - 1) / n, y = (p - 1) % n + 1;
    B[x][y] = false;
    int now = INF;
    for (int k = 0; k < 4; k++) {
      int u = x + dx[k], v = y + dy[k];
      if (!valid(u, v)) {
        now = 0;
        continue;
      }
      int j = (u - 1) * n + v;
      now = _min(now, D[find(j)] + B[u][v]);
      if (!B[u][v]) unite(p, j);
    }
    D[find(p)] = now;
    ans += now;
  }
  cout << ans << '\n';
  return 0;
}
