#include <bits/stdc++.h>
using namespace std;
const int N = 501;
int n, m, a[N * N], pos[N * N];
int main() {
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    cin >> a[i];
    a[i]--;
    pos[a[i]] = i;
  }
  int ans = 0;
  for (int i = 1; i <= n * n; i++) {
    int x = a[i] / n, y = a[i] % n;
    int k1 = 0, k = 501;
    k1 = 0;
    for (int j = 0; j < x; j++)
      if (pos[j * n + y] > i) k1++;
    k = min(k, k1);
    k1 = 0;
    for (int j = x + 1; j < n; j++)
      if (pos[j * n + y] > i) k1++;
    k = min(k, k1);
    k1 = 0;
    for (int j = 0; j < y; j++)
      if (pos[x * n + j] > i) k1++;
    k = min(k, k1);
    k1 = 0;
    for (int j = y + 1; j < n; j++)
      if (pos[x * n + j] > i) k1++;
    k = min(k, k1);
    ans += k;
  }
  cout << ans;
  return 0;
}
