#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int a[n + 1][n + 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = 1;
  long long int ans = 0;
  for (int j = 1; j <= n * n; j++) {
    int x;
    cin >> x;
    int r = x / n + 1;
    if (x % n == 0) r--;
    int c = x % n;
    if (c == 0) c = n;
    a[r][c] = 0;
    int sm = 0, tot = n * n;
    for (int i = r + 1; i <= n; i++) sm += a[i][c];
    tot = min(tot, sm), sm = 0;
    for (int i = 1; i < r; i++) sm += a[i][c];
    tot = min(tot, sm), sm = 0;
    for (int i = c + 1; i <= n; i++) sm += a[r][i];
    tot = min(tot, sm), sm = 0;
    for (int i = 1; i < c; i++) sm += a[r][i];
    tot = min(tot, sm), sm = 0;
    ans += tot;
  }
  cout << ans << "\n";
  return 0;
}
