#include <bits/stdc++.h>
using namespace std;
long long int n;
long long int nn;
long long int parent[505050];
long long int value[505050];
bool live[505050];
inline void init_uf() {
  for (long long int i = 0; i < ((long long int)(505050)); i++) {
    parent[i] = i;
  }
}
inline long long int find(long long int x) {
  if (parent[x] == x)
    return x;
  else
    return (parent[x] = find(parent[x]));
}
inline long long int findVal(long long int x) {
  auto p = find(x);
  return value[p];
}
inline void union_(long long int x, long long int y) {
  long long int parent_x = find(x);
  long long int parent_y = find(y);
  value[parent_y] = min(value[parent_x], value[parent_y]);
  parent[parent_x] = parent_y;
}
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;
    }
  }
  init_uf();
  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];
    pair<long long int, long long int> p = toPos(pos);
    live[pos] = false;
    for (long long int j = 0; j < ((long long int)(4)); j++) {
      pair<long long int, long long int> adj = {p.first + dy[j],
                                                p.second + dx[j]};
      if (min(adj.first, adj.second) < 0 || max(adj.first, adj.second) >= n) {
        continue;
      }
      tres = min(tres, findVal(fromPos(adj)) + (live[fromPos(adj)] ? 1 : 0));
    }
    for (long long int j = 0; j < ((long long int)(4)); j++) {
      pair<long long int, long long int> adj = {p.first + dy[j],
                                                p.second + dx[j]};
      if (min(adj.first, adj.second) < 0 || max(adj.first, adj.second) >= n) {
        continue;
      }
      if (!live[fromPos(adj)]) {
        union_(pos, fromPos(adj));
      }
    }
    res += tres;
    if (0) {
      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[fromPos({j, k})]) {
            cout << findVal(j * n + k) << " ";
          } else {
            cout << "X ";
          }
        }
        cout << endl;
      }
    }
  }
  cout << res << endl;
  return 0;
}
