#include <bits/stdc++.h>
using namespace std;
int N;
int a[510][510];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
bool ok(int x) { return x >= 0 && x < N; }
void upd(int r, int c) {
  queue<pair<int, int> > q;
  q.push(make_pair(r, c));
  set<pair<int, int> > update;
  while (q.size()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    for (int k = 0; k < 4; ++k) {
      int nx = x + dx[k], ny = y + dy[k];
      if (ok(nx) && ok(ny) && a[nx][ny] > a[x][y]) {
        update.insert(make_pair(nx, ny));
        q.push(make_pair(nx, ny));
      }
    }
  }
  for (auto p : update) --a[p.first][p.second];
}
void displ() {
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
  cout << endl;
}
int main() {
  cin >> N;
  for (int i = 0; i < N * N; ++i) {
    int r = i / N, c = i % N;
    a[r][c] = min(min(r, c), min(N - 1 - r, N - 1 - c));
  }
  int ret = 0;
  for (int i = 0; i < N * N; ++i) {
    int x;
    cin >> x;
    --x;
    int r = x / N, c = x % N;
    ret += a[r][c];
    upd(r, c);
  }
  cout << ret << "\n";
  return 0;
}
