#include <bits/stdc++.h>
using namespace std;
mt19937 mrand(random_device{}());
const double PI = acos((double)-1);
const double eps = 1e-7;
const long long inf0 = 1023 * 1024 * 1024;
const long long inf = inf0 * inf0;
const long long mod = 1e9 + 7;
void solve();
void scan();
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed;
  cout.precision(15);
  solve();
  return 0;
}
const int N = 501;
bool used[N][N];
void solve() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) used[i][j] = true;
  }
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    cin >> p[i];
    p[i]--;
    int x = p[i] / n;
    int y = p[i] % n;
    int t = 2e9;
    int c = 0;
    for (int j = 0; j < y; j++) c += used[x][j];
    t = min(t, c);
    c = 0;
    for (int j = y + 1; j < n; j++) c += used[x][j];
    t = min(t, c);
    c = 0;
    for (int j = 0; j < x; j++) c += used[j][y];
    t = min(t, c);
    c = 0;
    for (int j = x + 1; j < n; j++) c += used[j][y];
    t = min(t, c);
    ans += t;
    used[x][y] = false;
  }
  cout << ans << '\n';
  return;
}
