#include <bits/stdc++.h>
using namespace std;
int n, x, l, c, s;
bool a[501][501];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = 1;
  for (int q = 1; q <= n * n; q++) {
    cin >> x;
    l = (x - 1) / n + 1;
    c = x % n;
    if (c == 0) c = n;
    a[l][c] = 0;
    int r = n, m;
    m = 0;
    for (int i = 1; i < l; i++) m += a[i][c];
    r = min(r, m);
    m = 0;
    for (int i = l + 1; i <= n; i++) m += a[i][c];
    r = min(r, m);
    m = 0;
    for (int i = 1; i < c; i++) m += a[l][i];
    r = min(r, m);
    m = 0;
    for (int i = c + 1; i <= n; i++) m += a[l][i];
    r = min(r, m);
    s += r;
  }
  cout << s << '\n';
  return 0;
}
