#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = ' ';
  while (!isdigit(ch)) {
    ch = getchar();
    if (ch == '-') f = -1;
  }
  while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int n, a[505][505], ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    int x = read();
    a[(x - 1) / n + 1][(x - 1) % n + 1] = i;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int s = 0, minn = n;
      for (int k = 1; k < j; k++) {
        if (a[i][k] > a[i][j]) s++;
      }
      minn = min(minn, s), s = 0;
      for (int k = j + 1; k <= n; k++) {
        if (a[i][k] > a[i][j]) s++;
      }
      minn = min(minn, s), s = 0;
      for (int k = 1; k < i; k++) {
        if (a[k][j] > a[i][j]) s++;
      }
      minn = min(minn, s), s = 0;
      for (int k = i + 1; k <= n; k++) {
        if (a[k][j] > a[i][j]) s++;
      }
      minn = min(minn, s), s = 0;
      ans += minn;
    }
  }
  cout << ans;
}
