#include <bits/stdc++.h>
using namespace std;
constexpr int kDirections = 4;
constexpr int kDx[kDirections] = {-1, +1, 0, 0};
constexpr int kDy[kDirections] = {0, 0, -1, +1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int& e : p) {
    cin >> e;
  }
  vector<vector<int>> used(n, vector<int>(n, 0));
  vector<int> value(n * n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      value[i * n + j] = min({i, j, n - i - 1, n - j - 1});
    }
  }
  vector<int> parent(n * n);
  iota(parent.begin(), parent.end(), 0);
  function<int(int)> get_parent = [&](int x) {
    return parent[x] == x ? x : parent[x] = get_parent(parent[x]);
  };
  long long result = 0;
  for (int e : p) {
    --e;
    int x = e / n;
    int y = e % n;
    for (int dir = 0; dir < kDirections; ++dir) {
      int nx = x + kDx[dir];
      int ny = y + kDy[dir];
      if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
      if (!used[nx][ny]) continue;
      int neighour = nx * n + ny;
      neighour = get_parent(neighour);
      if (neighour != e) {
        parent[neighour] = e;
        value[e] = min(value[e], value[neighour]);
      }
    }
    used[x][y] = 1;
    result += value[e];
  }
  cout << result << '\n';
}
