#include <bits/stdc++.h>
using namespace std;
long long int n;
long long int nn;
long long int value[505050];
bool live[505050];
inline pair<long long int, long long int> toPos(long long int x) {
  return {x / n, x % n};
}
inline long long int fromPos(pair<long long int, long long int> p) {
  return p.first * n + p.second;
}
long long int dx[] = {-1, 1, 0, 0};
long long int dy[] = {0, 0, -1, 1};
int main() {
  cin >> n;
  long long int nn = n * n;
  for (long long int i = 0; i < ((long long int)(n)); i++) {
    for (long long int j = 0; j < ((long long int)(n)); j++) {
      long long int dx = min(j, n - j - 1);
      long long int dy = min(i, n - i - 1);
      value[fromPos({i, j})] = min(dx, dy);
      live[fromPos({i, j})] = true;
    }
  }
  long long int res = 0;
  for (long long int i = 0; i < ((long long int)(n * n)); i++) {
    long long int pos;
    cin >> pos;
    pos--;
    long long int tres = value[pos];
    res += tres;
    live[pos] = false;
    priority_queue<pair<long long int, pair<long long int, long long int>>> pq;
    pq.push({-tres, {toPos(pos)}});
    while (!pq.empty()) {
      long long int now_cost = -pq.top().first;
      pair<long long int, long long int> now = pq.top().second;
      pq.pop();
      if (value[fromPos(now)] < now_cost) {
        continue;
      }
      if (value[fromPos(now)] == now_cost && fromPos(now) != pos) {
        continue;
      }
      value[fromPos(now)] = now_cost;
      for (long long int k = 0; k < ((long long int)(4)); k++) {
        pair<long long int, long long int> next = {now.first + dy[k],
                                                   now.second + dx[k]};
        if (min(next.first, next.second) < 0 ||
            max(next.first, next.second) >= n) {
          continue;
        }
        long long int next_cost = now_cost;
        if (live[fromPos(now)]) {
          next_cost++;
        }
        pq.push({-next_cost, next});
      }
    }
    if (0) {
      if (1) cout << i << "\t" << tres << endl;
      for (long long int j = 0; j < ((long long int)(n)); j++) {
        for (long long int k = 0; k < ((long long int)(n)); k++) {
          if (live[j * n + k]) {
            cout << value[j * n + k] << " ";
          } else {
            cout << "X ";
          }
        }
        cout << endl;
      }
    }
  }
  cout << res << endl;
  return 0;
}
