#include <bits/stdc++.h>
using namespace std;
int n, a[250001], dis[501][501], dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
bool f[501][501];
void bfs(int x, int y) {
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    q.pop();
    int xx = tmp.first, yy = tmp.second;
    for (int i = 0; i < 4; i++) {
      int tx = xx + dx[i], ty = yy + dy[i];
      if (tx < 1 || tx > n || ty < 1 || ty > n ||
          dis[xx][yy] + f[xx][yy] >= dis[tx][ty]) {
        continue;
      }
      dis[tx][ty] = dis[xx][yy];
      q.push(make_pair(tx, ty));
    }
  }
}
int main(int argc, char const *argv[]) {
  scanf("%d", &n);
  for (int i = 1; i <= n * n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dis[i][j] = min(min(i - 1, j - 1), min(n - i, n - j));
      f[i][j] = 1;
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int x = (a[i] - 1) / n + 1, y = a[i] % n ? a[i] % n : n;
    ans += dis[x][y];
    f[x][y] = 0;
    bfs(x, y);
  }
  printf("%lld", ans);
  return 0;
}
