#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> P(N * N);
  for (int i = 0; i < (P.size()); ++i) {
    cin >> P[i];
    --P[i];
  }
  vector<vector<int>> d(N, vector<int>(N));
  for (int i = 0; i < (N); ++i)
    for (int j = 0; j < (N); ++j) {
      d[i][j] = min(min(i, N - 1 - i), min(j, N - 1 - j));
    }
  int result = 0;
  set<pii> leaved;
  for (int p : P) {
    int i = p / N;
    int j = p % N;
    leaved.emplace(i, j);
    result += d[i][j];
    deque<pii> q;
    for (int k = 0; k < (4); ++k) {
      int in = i + dx[k];
      int jn = j + dy[k];
      if (0 <= in && in < N && 0 <= jn && jn < N && d[in][jn] > d[i][j]) {
        d[in][jn] = d[i][j];
        q.emplace_back(in, jn);
      }
    }
    while (!q.empty()) {
      auto [x, y] = q.front();
      q.pop_front();
      for (int k = 0; k < (4); ++k) {
        int in = x + dx[k];
        int jn = y + dy[k];
        int d_next = d[x][y] + (leaved.count(pii(x, y)) ? 0 : 1);
        if (0 <= in && in < N && 0 <= jn && jn < N && d[in][jn] > d_next) {
          d[in][jn] = d_next;
          if (d_next == d[x][y]) {
            q.emplace_front(in, jn);
          } else {
            q.emplace_back(in, jn);
          }
        }
      }
    }
  }
  cout << result << endl;
  return 0;
}
