#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
void solve();
void getInput();
int64_t N;
vector<vector<int64_t>> C;
vector<vector<bool>> O;
vector<pair<int64_t, int64_t>> operations;
void getInput() {
  cin >> N;
  operations = vector<pair<int64_t, int64_t>>(N * N);
  for (int64_t i = 0; i < N * N; ++i) {
    int64_t num;
    cin >> num;
    num--;
    operations[i] = {num / N, num % N};
  }
  O = vector<vector<bool>>(N, vector<bool>(N, true));
  C = vector<vector<int64_t>>(N, vector<int64_t>(N, 0));
  for (int64_t i = 0; i < N; ++i) {
    for (int64_t j = 0; j < N; ++j) {
      int64_t a = min(i, N - 1 - i);
      int64_t b = min(j, N - 1 - j);
      C[i][j] = min(a, b);
    }
  }
}
void updateNeighbors(pair<int64_t, int64_t> pos) {
  vector<int64_t> ret;
  queue<pair<int64_t, int64_t>> que;
  que.push(pos);
  int64_t cost = C[pos.first][pos.second];
  vector<vector<bool>> visit(N, vector<bool>(N, false));
  while (!que.empty()) {
    auto op = que.front();
    que.pop();
    if (visit[op.first][op.second]) continue;
    visit[op.first][op.second] = true;
    vector<int64_t> dx = {1, -1, 0, 0};
    vector<int64_t> dy = {0, 0, 1, -1};
    for (int64_t i = 0; i < 4; ++i) {
      int64_t x = op.first + dx[i];
      int64_t y = op.second + dy[i];
      if (x < 0 or y < 0 or x >= N or y >= N) continue;
      if (O[x][y]) {
        C[x][y] = min(C[x][y], cost);
      } else {
        que.push({x, y});
      }
    }
  }
}
void solve() {
  int64_t ans;
  for (auto op : operations) {
    ans += C[op.first][op.second];
    O[op.first][op.second] = false;
    updateNeighbors(op);
  }
  cout << ans << endl;
}
signed main() {
  getInput();
  solve();
  return 0;
}
