#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
int p[N * N], a[N][N], h[N][N], n;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int id(int i, int j) { return (i - 1) * n + j; }
int main() {
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    for (int j = 1; j <= n * n; j++) {
      h[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
    }
  }
  for (int i = 1; i <= n * n; i++) {
    int pos;
    scanf("%d", &pos);
    int row = (pos - 1) / n + 1, col = (pos - 1) % n + 1;
    ans += h[row][col];
    queue<pair<int, int>> Q;
    Q.push({row, col});
    a[row][col] = 1;
    while (!Q.empty()) {
      auto t = Q.front();
      Q.pop();
      int cost = h[t.first][t.second] + (!a[t.first][t.second]);
      for (int j = 0; j < 4; j++) {
        int x = t.first + dx[j], y = t.second + dy[j];
        if (h[x][y] > cost) {
          h[x][y] = cost;
          Q.push({x, y});
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
