#include <bits/stdc++.h>
using namespace std;
const long long int INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const long long int mod = 1000000007ll;
const int mod_int = 1000000007;
long long int N;
long long int P[250000];
long long int ans = 0;
void solve() {
  vector<pair<long long int, long long int>> P_point(N * N + 1);
  vector<vector<long long int>> table(N, vector<long long int>(N));
  for (int h = 0; h < N; h++) {
    for (int w = 0; w < N; w++) {
      long long int upper = h;
      long long int bottom = N - 1 - h;
      long long int left = w;
      long long int right = N - 1 - w;
      long long int vertical = min(upper, bottom);
      long long int horizontal = min(left, right);
      table[h][w] = min(vertical, horizontal);
      P_point[h * N + w + 1] = pair<long long int, long long int>(h, w);
    }
  }
  auto show_table = [&]() {
    for (int h = 0; h < N; h++) {
      for (int w = 0; w < N; w++) {
        cout << table[h][w] << " ";
      }
      cout << endl;
    }
    cout << endl;
  };
  vector<vector<long long int>> seat(N, vector<long long int>(N, 1));
  for (int i = 0; i < N * N; i++) {
    long long int h = P_point[P[i]].first;
    long long int w = P_point[P[i]].second;
    ans += table[h][w];
    seat[h][w] = 0;
    long long int dislike = table[h][w];
    queue<pair<long long int, long long int>> q;
    q.push(pair<long long int, long long int>(h, w));
    map<pair<long long int, long long int>, bool> used;
    while (!q.empty()) {
      long long int now_h = q.front().first;
      long long int now_w = q.front().second;
      q.pop();
      used[pair<long long int, long long int>(now_h, now_w)] = true;
      table[h][w] = dislike;
      long long int dh[4] = {-1, 1, 0, 0};
      long long int dw[4] = {0, 0, -1, 1};
      for (int d = 0; d < 4; d++) {
        long long int next_h = now_h + dh[d];
        long long int next_w = now_w + dw[d];
        if (next_h <= N - 1 && next_h >= 0 && next_w <= N - 1 && next_w >= 0) {
          if (seat[next_h][next_w]) {
            table[next_h][next_w] = min(table[next_h][next_w], dislike);
          } else {
            if (table[next_h][next_w] > dislike &&
                !used[pair<long long int, long long int>(next_h, next_w)]) {
              q.push(pair<long long int, long long int>(next_h, next_w));
            }
          }
        }
      }
    }
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
  }
  solve();
  cout << ans << endl;
  return 0;
}
