#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = ll(1e18) + 5;
ll N;
bool onBoard(int y, int x) {
  if (y >= 0 && y < N && x >= 0 && x < N) {
    return true;
  }
  return false;
}
int main() {
  cin >> N;
  vector<vector<ll>> num(N);
  vector<vector<bool>> exist(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int val1 = min(i + 1, j + 1);
      int val2 = min(N - i, N - j);
      num[i].push_back(min(val1 - 1, val2 - 1));
      exist[i].push_back(true);
    }
  }
  int dx[] = {0, 1, 0, -1};
  int dy[] = {1, 0, -1, 0};
  ll ans = 0;
  for (int q = 0; q < N * N; q++) {
    int inP;
    cin >> inP;
    --inP;
    int y = inP / N;
    int x = inP % N;
    ll temp = num[y][x];
    ans += temp;
    exist[y][x] = false;
    queue<P> pq;
    pq.push(P(y, x));
    while (!pq.empty()) {
      P p = pq.front();
      int py = p.first;
      int px = p.second;
      pq.pop();
      if (exist[py][px]) {
        continue;
      }
      for (int i = 0; i < 4; i++) {
        int ny = py + dy[i];
        int nx = px + dx[i];
        if (!onBoard(ny, nx)) {
          continue;
        }
        if (num[ny][nx] > num[py][px]) {
          num[ny][nx] = num[py][px];
          pq.push(P(ny, nx));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
