#include <bits/stdc++.h>
using namespace std;
long long n, hate;
long long d[505][505], m[505][505], pl[505][505];
long long p[2505];
const long long dx[4] = {-1, 1, 0, 0};
const long long dy[4] = {0, 0, -1, 1};
void bc1(long long d[505][505]) {
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= n; j++) d[i][j] = 1;
}
void bc0(long long m[505][505]) {
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= n; j++) m[i][j] = 0;
}
void bceve(long long a[505][505], long long b[505][505]) {
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= n; j++) a[i][j] = b[i][j];
}
long long dfs(long long nx, long long ny) {
  if (d[nx][ny] == 1) {
    m[nx][ny] = 1;
    return 0;
  }
  if (nx == 0 || nx == n - 1 || ny == 0 || ny == n - 1) return 1;
  pl[nx][ny] = 1;
  for (long long i = 0; i < 4; i++) {
    if (!pl[nx + dx[i]][ny + dy[i]])
      if (dfs(nx + dx[i], ny + dy[i])) return 1;
  }
  return 0;
}
void calc(long long nx, long long ny) {
  d[nx][ny] = 0;
  long long dd[505][505];
  bceve(dd, d);
  long long ans = 0;
  while (true) {
    cout << dfs(nx, ny);
    if (dfs(nx, ny)) break;
    for (long long i = 0; i <= n; i++)
      for (long long j = 0; j <= n; j++)
        if (m[i][j] == 1) dd[i][j] = 0;
    ans++;
    bc0(pl);
  }
  bc0(m);
  hate += ans;
}
signed main() {
  bc1(d);
  cin >> n;
  for (long long i = 0; i < n * n; i++) cin >> p[i];
  for (long long i = 0; i < n * n; i++) {
    p[i]--;
    long long nx = p[i] / n;
    long long ny = p[i] % n;
    calc(nx, ny);
  }
  cout << hate;
  return 0;
}
