#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
int f[maxn][maxn];
int a[maxn * maxn];
bool mark[maxn][maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n = 0;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> a[i];
    a[i]--;
    mark[i / n][i % n] = false;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      f[i][j] = min(i, min(j, min(n - i - 1, n - j - 1)));
    }
  }
  int res = 0;
  for (int i = 0; i < n * n; i++) {
    int v = a[i];
    int x = v / n;
    int y = v % n;
    int _x;
    int _y;
    mark[x][y] = true;
    _x = x;
    _y = y;
    int cnt = 0;
    while (_x > 0) {
      f[x][y] = min(f[x][y], cnt + f[_x][_y] + !mark[_x][_y]);
      cnt += (!mark[_x][_y]);
      _x--;
    }
    _x = x;
    _y = y;
    cnt = 0;
    while (_x < n) {
      f[x][y] = min(f[x][y], cnt + f[_x][_y] + !mark[_x][_y]);
      cnt += (!mark[_x][_y]);
      _x++;
    }
    _x = x;
    _y = y;
    cnt = 0;
    while (_y > 0) {
      f[x][y] = min(f[x][y], cnt + f[_x][_y] + !mark[_x][_y]);
      cnt += (!mark[_x][_y]);
      _y--;
    }
    _x = x;
    _y = y;
    cnt = 0;
    while (_y < n) {
      f[x][y] = min(f[x][y], cnt + f[_x][_y] + !mark[_x][_y]);
      cnt += (!mark[_x][_y]);
      _y++;
    }
    _x = x;
    _y = y;
    res += f[x][y];
    _x = x;
    _y = y;
    cnt = 0;
    while (_x > 0) {
      f[_x][_y] = min(f[_x][_y], cnt + f[x][y]);
      cnt += (!mark[_x][_y]);
      _x--;
    }
    _x = x;
    _y = y;
    cnt = 0;
    while (_x < n) {
      f[_x][_y] = min(f[_x][_y], cnt + f[x][y]);
      cnt += (!mark[_x][_y]);
      _x++;
    }
    _x = x;
    _y = y;
    cnt = 0;
    while (_y > 0) {
      f[_x][_y] = min(f[_x][_y], cnt + f[x][y]);
      cnt += (!mark[_x][_y]);
      _y--;
    }
    _x = x;
    _y = y;
    cnt = 0;
    while (_y < n) {
      f[_x][_y] = min(f[_x][_y], cnt + f[x][y]);
      cnt += (!mark[_x][_y]);
      _y++;
    }
  }
  cout << res;
  return 0;
}
