#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
inline long long read() {
  char ch = getchar();
  long long x = 0, f = 0;
  while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int n, ans;
bool vis[maxn][maxn];
int main() {
  n = read();
  for (int _ = (0); _ <= (n * n - 1); _++) {
    int v = read() - 1;
    int x = v / n, y = v % n, s1 = 0, s2 = 0, s3 = 0, s4 = 0;
    for (int i = (0); i <= (x - 1); i++) s1 += !vis[i][y];
    for (int i = (0); i <= (y - 1); i++) s2 += !vis[x][i];
    for (int i = (x + 1); i <= (n - 1); i++) s3 += !vis[i][y];
    for (int i = (y + 1); i <= (n - 1); i++) s4 += !vis[x][i];
    ans += min(min(s1, s2), min(s3, s4));
    vis[x][y] = true;
  }
  printf("%d\n", ans);
}
