#include <bits/stdc++.h>
using namespace std;
const long long maxn = 510;
const long long INF = 1e18;
long long n, p[maxn * maxn], dis[maxn * maxn], rr, ans = 0;
vector<long long> g[maxn * maxn];
bool mark[maxn];
long long ver(long long i, long long j) {
  if (i < 1 || i > n || j < 1 || j > n) return rr;
  return ((i - 1) * n) + j;
}
void bfs(long long root) {
  deque<long long> qu;
  qu.push_back(root);
  while (qu.size()) {
    long long v = qu.front();
    qu.pop_front();
    for (auto uu : g[v]) {
      long long u = uu, w = mark[v];
      if ((dis[v] + w) < dis[u]) {
        dis[u] = dis[v] + w;
        if (w == 0)
          qu.push_front(u);
        else
          qu.push_back(u);
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (long long i = 1; i <= n * n; i++) {
    cin >> p[i];
    dis[i] = INF;
    mark[i] = true;
  }
  rr = (n * n) + 1;
  dis[rr] = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      long long v = ((i - 1) * n) + j;
      long long v1 = ver(i - 1, j), v2 = ver(i + 1, j), v3 = ver(i, j + 1),
                v4 = ver(i, j - 1);
      g[v].push_back(v1);
      g[v1].push_back(v);
      g[v].push_back(v2);
      g[v2].push_back(v);
      g[v].push_back(v3);
      g[v3].push_back(v);
      g[v].push_back(v4);
      g[v4].push_back(v);
    }
  }
  bfs(rr);
  for (long long i = 1; i <= n * n; i++) {
    ans += dis[p[i]];
    mark[p[i]] = false;
    bfs(p[i]);
  }
  cout << ans;
  return 0;
}
