#include <bits/stdc++.h>
using namespace std;
int n;
bool a[555][555];
bool b[555][555];
int memo[555][555];
int solve(int y, int x) {
  if (y == 0 || x == 0 || y > n || x > n) return memo[y][x] = 0;
  if (memo[y][x] != 1 << 28) return memo[y][x];
  if (b[y][x]) return 1 << 28;
  int ret = 0;
  b[y][x] = true;
  ret += min(solve(y + 1, x),
             min(solve(y - 1, x), min(solve(y, x + 1), solve(y, x - 1)))) +
         a[y][x];
  b[y][x] = false;
  return memo[y][x] = ret;
}
int main() {
  int ans = 0;
  cin >> n;
  for (int i = 0; i < 555 * 555; i++) a[i / 555][i % 555] = 1;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int y = p / n, x = p % n;
    y++;
    x++;
    a[y][x] = 0;
    fill(memo[0], memo[555], 1 << 28);
    solve(y, x);
    ans += memo[y][x];
  }
  cout << ans << endl;
}
