#include <bits/stdc++.h>
const int P = 1000000007;
const int N = 505;
using namespace std;
int n, p[N * N];
int d[N][N], o[N][N];
priority_queue<pair<int, pair<int, int> > > q;
void upd() {
  while (!q.empty()) {
    pair<int, pair<int, int> > c = q.top();
    q.pop();
    c.first = -c.first;
    int i = c.second.first, j = c.second.second;
    if (d[i][j] < c.first) continue;
    d[i][j] = c.first;
    if (d[i][j] + o[i - 1][j] < d[i - 1][j])
      q.push({-d[i][j] - o[i - 1][j], {i - 1, j}});
    if (d[i][j] + o[i + 1][j] < d[i + 1][j])
      q.push({-d[i][j] - o[i + 1][j], {i + 1, j}});
    if (d[i][j] + o[i][j - 1] < d[i][j - 1])
      q.push({-d[i][j] - o[i][j - 1], {i, j - 1}});
    if (d[i][j] + o[i][j + 1] < d[i][j + 1])
      q.push({-d[i][j] - o[i][j + 1], {i, j + 1}});
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> p[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      o[i][j] = 1;
      d[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
    }
  }
  int ans = 0;
  for (int k = 0; k < n * n; k++) {
    int i = 1 + (p[k] - 1) / n, j = 1 + (p[k] - 1) % n;
    ans += d[i][j] - 1;
    o[i][j] = 0;
    q.push({-(d[i][j] - 1), {i, j}});
    upd();
  }
  cout << ans << endl;
  return 0;
}
