#include <bits/stdc++.h>
using namespace std;
int a[555][555], b[555][555], d[555][555];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) a[i][j] = min({i, n - i - 1, j, n - j - 1});
  long r = 0;
  for (int k = 0; k < n * n; k++) {
    int p;
    scanf("%d", &p);
    p--;
    r += a[p / n][p % n];
    b[p / n][p % n]++;
    queue<pair<int, int>> q;
    q.emplace(p / n, p % n);
    for (int i = 0; i < n; i++) fill(d[i], d[i] + n, 0);
    d[p / n][p % n] = 1;
    while (!q.empty()) {
      auto [i, j] = q.front();
      q.pop();
      if (i && !d[i - 1][j] && a[i - 1][j] > a[i][j] + 1 - b[i][j])
        d[i - 1][j] = 1, a[i - 1][j] = a[i][j] + 1 - b[i][j],
              q.emplace(i - 1, j);
      if (i + 1 < n && !d[i + 1][j] && a[i + 1][j] > a[i][j] + 1 - b[i][j])
        d[i + 1][j] = 1, a[i + 1][j] = a[i][j] + 1 - b[i][j],
              q.emplace(i + 1, j);
      if (j && !d[i][j - 1] && a[i][j - 1] > a[i][j] + 1 - b[i][j])
        d[i][j - 1] = 1, a[i][j - 1] = a[i][j] + 1 - b[i][j],
                 q.emplace(i, j - 1);
      if (j + 1 < n && !d[i][j + 1] && a[i][j + 1] > a[i][j] + 1 - b[i][j])
        d[i][j + 1] = 1, a[i][j + 1] = a[i][j] + 1 - b[i][j],
                 q.emplace(i, j + 1);
    }
  }
  cout << r << endl;
  return 0;
}
