#include <bits/stdc++.h>
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 5e2 + 50;
const long long mod = 1e9 + 7;
using namespace std;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}, n, p[nmax * nmax],
    f[nmax][nmax], a[nmax][nmax], x, y, nx, ny, v[nmax][nmax], i, j, rs, vl;
pair<int, int> pr[nmax * nmax];
queue<pair<int, int> > q;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n;
  for (i = 1; i <= n * n; i++) cin >> p[i];
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      f[i][j] = min(min(i - 1, j - 1), min(n - i, n - j)), a[i][j] = 1,
      pr[n * (i - 1) + j] = make_pair(i, j);
  for (i = 1; i <= n * n; i++) {
    x = pr[p[i]].first, y = pr[p[i]].second;
    rs += f[x][y];
    a[x][y] = 0;
    q.push(make_pair(x, y));
    v[i][j] = 1;
    while (!q.empty()) {
      x = q.front().first, y = q.front().second;
      q.pop();
      v[x][y] = 0;
      vl = f[x][y] + a[x][y];
      for (j = 0; j < 4; j++) {
        nx = x + dx[j], ny = y + dy[j];
        if (nx < 1 || nx > n || ny < 1 || ny > n || f[nx][ny] <= vl) continue;
        q.push(make_pair(nx, ny));
        f[nx][ny] = vl;
        v[nx][ny] = 1;
      }
    }
  }
  cout << rs << endl;
  return 0;
}
