#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
template <class t>
inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f) x = -x;
  return x;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
template <class t>
inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
const int N = 505 * 505;
int ans, n, a[N], v[N], L[N], R[N], U[N], D[N];
signed main() {
  read(n);
  for (int i = 1; i <= n * n; i++) read(a[i]), v[i] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 2; j <= n; j++) L[(i - 1) * n + j] = (i - 1) * n + j - 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < n; j++) R[(i - 1) * n + j] = (i - 1) * n + j + 1;
  for (int i = 2; i <= n; i++)
    for (int j = 1; j <= n; j++) U[(i - 1) * n + j] = (i - 2) * n + j;
  for (int i = 1; i < n; i++)
    for (int j = 1; j <= n; j++) D[(i - 1) * n + j] = i * n + j;
  for (int i = 1; i <= n * n; i++) {
    int mi = n, cnt = 0;
    v[a[i]] = 0;
    for (int j = a[i]; j; j = U[j]) cnt += v[j];
    mi = min(mi, cnt);
    cnt = 0;
    for (int j = a[i]; j; j = D[j]) cnt += v[j];
    mi = min(mi, cnt);
    cnt = 0;
    for (int j = a[i]; j; j = L[j]) cnt += v[j];
    mi = min(mi, cnt);
    cnt = 0;
    for (int j = a[i]; j; j = R[j]) cnt += v[j];
    mi = min(mi, cnt);
    cnt = 0;
    ans += mi;
  }
  write(ans);
}
