#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
vector<long long> a[501];
long long c[250001], r[250001], n, close[250001], dist = 0;
int dfs(long long v, vector<bool> vis) {
  vis[v] = true;
  dist = min(dist, close[v]);
  if (c[v] == 1) return 0;
  int f = 1;
  for (int u : a[v]) {
    if (!vis[u]) f = dfs(u, vis);
    if (f == 0) return 0;
  }
  return 1;
}
long long up(long long i) {
  if (i <= n)
    return 0;
  else
    return i - n;
}
long long down(long long i) {
  if (i > n * n - n)
    return 0;
  else
    return i + n;
}
long long left(long long i) {
  if (i % n == 1)
    return 0;
  else
    return i - 1;
}
long long right(long long i) {
  if (i % n == 0)
    return 0;
  else
    return i + 1;
}
void make(long long u) {
  r[u] = 1;
  if (right(u)) {
    long long v = right(u);
    if (r[v]) {
      a[u].push_back(v);
      a[v].push_back(u);
    }
  }
  if (left(u)) {
    long long v = left(u);
    if (r[v]) {
      a[u].push_back(v);
      a[v].push_back(u);
    }
  }
  if (up(u)) {
    long long v = up(u);
    if (r[v]) {
      a[u].push_back(v);
      a[v].push_back(u);
    }
  }
  if (down(u)) {
    long long v = down(u);
    if (r[v]) {
      a[u].push_back(v);
      a[v].push_back(u);
    }
  }
}
void closer(int n) {
  for (long long i = 1; i <= n * n; i++) {
    long long u = i, c1 = 0, ans = 9223372036854775807;
    u = i;
    while (u) {
      u = right(u);
      c1++;
    }
    ans = min(c1, ans);
    u = i;
    c1 = 0;
    while (u) {
      u = left(u);
      c1++;
    }
    ans = min(c1, ans);
    u = i;
    c1 = 0;
    while (u) {
      u = up(u);
      c1++;
    }
    ans = min(c1, ans);
    u = i;
    c1 = 0;
    while (u) {
      u = down(u);
      c1++;
    }
    ans = min(c1, ans);
    close[i] = ans - 1;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  vector<long long> p(n * n);
  closer(n);
  for (long long i = 0; i < n * n; i++) cin >> p[i];
  for (long long i = 1; i <= n; i++) c[i] = 1;
  for (long long i = 1; i < n * n; i += n) c[i] = 1;
  for (long long i = n; i <= n * n; i += n) c[i] = 1;
  for (long long i = n * n; i > n * n - n; i--) c[i] = 1;
  long long ans = 0;
  for (long long i = 0; i < p.size(); i++) {
    make(p[i]);
    vector<bool> vis(n * n + 1, false);
    dist = 9223372036854775807;
    long long h = dfs(p[i], vis);
    ans += dist;
  }
  cout << ans;
  return 0;
}
