#include <bits/stdc++.h>
using namespace std;
long long ans = 0;
long long n;
bool inarea(long long x, long long y) {
  return x >= 0 && y >= 0 && x < n && y < n;
}
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {1, 0, -1, 0};
int main() {
  cin >> n;
  vector<vector<long long>> v(n, vector<long long>(n, 0));
  for (long long i = 0; i < (long long)(n); i++)
    for (long long j = 0; j < (long long)(n); j++) {
      v[i][j] = min({i + 1, n - i, j + 1, n - j});
    }
  for (long long i = 0; i < (long long)(n * n); i++) {
    long long a;
    cin >> a;
    a--;
    long long x = a / n, y = a % n;
    ans += v[x][y];
    queue<pair<long long, long long>> q;
    q.push({x, y});
    v[x][y]--;
    while (!q.empty()) {
      auto r = q.front();
      q.pop();
      for (long long j = 0; j < (long long)(4); j++)
        if (inarea(r.first + dx[j], r.second + dy[j]) &&
            v[r.first][r.second] + 1 < v[r.first + dx[j]][r.second + dy[j]]) {
          v[r.first + dx[j]][r.second + dy[j]] = v[r.first][r.second] + 1;
          q.push({r.first + dx[j], r.second + dy[j]});
        }
    }
  }
  cout << ans - n * n << endl;
  return 0;
}
