#include <bits/stdc++.h>
using namespace std;
int main(void) {
  const vector<pair<int, int>> diffs = {make_pair(-1, 0), make_pair(0, -1),
                                        make_pair(0, 1), make_pair(1, 0)};
  int n;
  cin >> n;
  const int sq_n = n * n;
  vector<int> orders(sq_n);
  for (size_t i = 0; i < sq_n; i++) {
    cin >> orders[i];
  }
  auto rems = vector<vector<bool>>(n, vector<bool>(n, true));
  auto seats = vector<vector<int>>(n, vector<int>(n, 1));
  const int half_n = n / 2;
  for (size_t i = 1; i <= n / 2; i++) {
    for (size_t j = i; j < n - i; j++) {
      seats[i][j] = seats[n - 1 - i][j] = seats[j][i] = seats[j][n - 1 - i] =
          i + 1;
    }
  }
  long long ans = 0;
  for (auto &&order : orders) {
    int row = (order - 1) / n;
    int col = (order - 1) % n;
    ans += seats[row][col] - 1;
    rems[row][col] = false;
    if (row == 0 || row == n - 1 || col == 0 || col == n - 1) {
      seats[row][col] = 0;
    } else {
      long long min_cost = seats[row][col];
      for (auto &&diff : diffs) {
        const int nei_row = row + diff.first;
        const int nei_col = col + diff.second;
        if (0 <= row && row < n && 0 <= col && col < n) {
          if (seats[nei_row][nei_col] < min_cost) {
            min_cost = seats[nei_row][nei_col];
          }
        }
      }
      seats[row][col] = min_cost;
    }
    auto opening = queue<pair<int, int>>({make_pair(row, col)});
    while (!opening.empty()) {
      const auto curr_pos = opening.front();
      opening.pop();
      for (auto &&diff : diffs) {
        const int next_row = curr_pos.first + diff.first;
        const int next_col = curr_pos.second + diff.second;
        if (!(0 <= next_row && next_row < n && 0 <= next_col && next_col < n)) {
          continue;
        }
        const int next_cost =
            seats[curr_pos.first][curr_pos.second] + rems[next_row][next_col];
        if (next_cost < seats[next_row][next_col]) {
          seats[next_row][next_col] = next_cost;
          opening.push(make_pair(next_row, next_col));
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
