#include <bits/stdc++.h>
const int INF = 1111111111;
using namespace std;
struct UnionFind {
  vector<int> par;
  UnionFind(int N) : par(N) {
    for (int i = 0; i < N; i++) par[i] = i;
  }
  int root(int x) {
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry) return;
    par[rx] = ry;
  }
  bool same(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};
int n;
const int N = 555;
int p[N];
int cost[N];
bool used[N];
int getId(int y, int x) { return y * n + x + 1; }
void init() {
  cin >> n;
  for (int i = (0); i < (int)(n * n); i++) cin >> p[i];
}
void solve() {
  UnionFind uf = UnionFind(n * n);
  for (int i = (0); i < (int)(n); i++) {
    for (int j = (0); j < (int)(n); j++) {
      int id = getId(i, j);
      cost[id] = min(min(i, j), min(n - i - 1, n - j - 1)) + 1;
    }
  }
  int answer = 0;
  cost[0] = 0;
  used[0] = true;
  for (int i = (0); i < (int)(n * n); i++) {
    int t = p[i];
    int up = t - n;
    int down = t + n;
    int right = t + 1;
    int left = t - 1;
    used[t] = true;
    int uproot = up >= 1 ? uf.root(up) : 0;
    int downroot = down <= n * n ? uf.root(down) : 0;
    int leftroot = t % n != 1 ? uf.root(left) : 0;
    int rightroot = t % n != 0 ? uf.root(right) : 0;
    int upcost = cost[uproot];
    int downcost = cost[downroot];
    int leftcost = cost[leftroot];
    int rightcost = cost[rightroot];
    int mincost = min(min(upcost, downcost), min(leftcost, rightcost));
    answer += mincost;
    int unite = t;
    if (upcost == mincost) unite = uproot;
    if (downcost == mincost) unite = downroot;
    if (leftcost == mincost) unite = leftroot;
    if (rightcost == mincost) unite = rightroot;
    uf.unite(t, unite);
    if (used[uproot]) uf.unite(uproot, unite);
    if (used[downroot]) uf.unite(downroot, unite);
    if (used[leftroot]) uf.unite(leftroot, unite);
    if (used[rightroot]) uf.unite(rightroot, unite);
  }
  cout << answer << endl;
}
int main() {
  init();
  solve();
}
