#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const int N = 500;
int n;
bool hav[N + 1][N + 1];
int ans[N + 1][N + 1];
pair<int, int> q[N * N + 1];
int head, tail;
const int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main() {
  cin >> n;
  int anss = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      ans[i][j] = min(min(i, n - i + 1), min(j, n - j + 1)), hav[i][j] = true;
  for (int i = 1; i <= n * n; i++) {
    int ord;
    cin >> ord;
    int x = (ord + n - 1) / n, y = (ord - 1) % n + 1;
    ans[x][y]--;
    hav[x][y] = false;
    anss += ans[x][y];
    head = tail;
    q[tail++] = make_pair(x, y);
    while (head < tail) {
      int xx = q[head].first, yy = q[head].second;
      head++;
      for (int j = 0; j < 4; j++) {
        int xxx = xx + dx[j], yyy = yy + dy[j];
        if (1 <= xxx && xxx <= n && 1 <= yyy && yyy <= n &&
            ans[xx][yy] + hav[xxx][yyy] < ans[xxx][yyy])
          ans[xxx][yyy] = ans[xx][yy] + hav[xxx][yyy],
          q[tail++] = make_pair(xxx, yyy);
      }
    }
  }
  cout << anss;
  return 0;
}
