#include <bits/stdc++.h>
using namespace std;
int matrix[507][507];
bool dlt[507][507];
int n;
void dfs(int a, int b) {
  int Q = 1e9;
  for (int dx = -1; dx <= 1; ++dx) {
    for (int dy = -1; dy <= 1; ++dy) {
      if (abs(dx) + abs(dy) != 1) continue;
      int c = a + dx, d = b + dy;
      if (c < 0 || c >= n || d < 0 || d >= n) continue;
      int val = matrix[a][b] + (!dlt[c][d]);
      if (val < matrix[c][d]) {
        matrix[c][d] = val;
        dfs(c, d);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      int moves = min({i + 1, n - i, j + 1, n - j});
      matrix[i][j] = moves;
    }
  }
  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int first;
    cin >> first;
    first--;
    int a = first / n, b = first % n;
    dlt[a][b] = true;
    ans += max(0, matrix[a][b] - 1);
    int Q = 1e9;
    for (int dx = -1; dx <= 1; ++dx) {
      for (int dy = -1; dy <= 1; ++dy) {
        if (abs(dx) + abs(dy) != 1) continue;
        int c = a + dx, d = b + dy;
        if (c < 0 || c >= n || d < 0 || d >= n) continue;
        Q = min(Q, matrix[c][d]);
      }
    }
    if (a == 0 || a == n - 1 || b == 0 || b == n - 1) Q = 0;
    matrix[a][b] = Q;
    dfs(a, b);
  }
  cout << ans;
}
