#include <bits/stdc++.h>
using namespace std;
long long n, hate;
long long d[505][505];
long long p[2505];
void bc1() {
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= n; j++) d[i][j] = 1;
}
void calc(long long nx, long long ny) {
  long long ans = 1e9, x = 0;
  d[nx][ny] = 0;
  for (long long i = 0; i < nx; i++) {
    if (d[i][ny]) x++;
  }
  ans = min(ans, x);
  x = 0;
  for (long long i = nx + 1; i < n; i++) {
    if (d[i][ny]) x++;
  }
  ans = min(ans, x);
  x = 0;
  for (long long i = 0; i < ny; i++) {
    if (d[nx][i]) x++;
  }
  ans = min(ans, x);
  x = 0;
  for (long long i = ny + 1; i < n; i++) {
    if (d[nx][i]) x++;
  }
  ans = min(ans, x);
  hate += ans;
}
signed main() {
  bc1();
  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;
}
