#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
int di[4] = {0, 1, 0, -1};
int dj[4] = {1, 0, -1, 0};
int main() {
  int n;
  cin >> n;
  vi p(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> p[i];
    --p[i];
  }
  vector<vector<int> > iru(n, vector<int>(n, 1));
  vvi mat(n, vi(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      mat[i][j] = min({i, j, n - 1 - i, n - 1 - j});
    }
  }
  int ans = 0;
  vector<P> que;
  for (int iii = 0; iii < n * n; ++iii) {
    int i = p[iii] / n, j = p[iii] % n;
    ans += mat[i][j];
    iru[i][j] = 0;
    que.clear();
    que.emplace_back(i, j);
    vector<vector<bool> > used(n, vector<bool>(n, false));
    used[i][j] = true;
    for (int v = 0; v < (int)que.size(); ++v) {
      int ni, nj;
      ni = que[v].first;
      nj = que[v].second;
      used[ni][nj] = false;
      int val = mat[ni][nj] + iru[ni][nj];
      for (int k = 0; k < 4; ++k) {
        int nni = ni + di[k], nnj = nj + dj[k];
        if (nni < 0 || nnj < 0 || nni > n - 1 || nnj > n - 1) continue;
        if (mat[nni][nnj] > val) {
          mat[nni][nnj] = val;
          if (!used[nni][nnj]) {
            que.emplace_back(nni, nnj);
            used[nni][nnj] = true;
          }
        }
      }
    }
  }
  cout << ans << endl;
}
