#include <bits/stdc++.h>
using namespace std;
const long long int maxr = 505;
long long int n, health[maxr][maxr];
bool done[maxr][maxr];
vector<long long int> dx = {0, 0, -1, 1};
vector<long long int> dy = {1, -1, 0, 0};
long long int update(long long int x, long long int y) {
  for (long long int i = 0; i < 4; i++) {
    long long int nx = x + dx[i];
    long long int ny = y + dy[i];
    if (0 > nx || nx >= n || 0 > ny || ny >= n) continue;
    long long int co = !done[x][y];
    if (health[nx][ny] > health[x][y] + co) {
      health[nx][ny] = health[x][y] + co;
      update(nx, ny);
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < n; j++) {
      health[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
      done[i][j] = false;
    }
  }
  long long int ans = 0;
  for (long long int i = 0; i < n * n; i++) {
    long long int p;
    cin >> p;
    long long int x = p / n;
    long long int y = p % n;
    y--;
    if (y == -1) {
      x--;
      y = n - 1;
    }
    ans += health[x][y];
    done[x][y] = true;
    update(x, y);
  }
  cout << ans << endl;
  return 0;
}
