#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const double PI = acos(-1);
const int MAX = 200010;
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int>> s(n, vector<int>(n, 1));
  int ans = 0;
  for (int k = 0; k < n * n; k++) {
    int p;
    cin >> p;
    --p;
    int i = p / n, j = p % n;
    s[i][j] = 0;
    int tans1 = 0, tans2 = 0, tans3 = 0, tans4 = 0;
    for (int l = 0; l < i; l++) tans1 += s[l][j];
    for (int l = i + 1; l < n; l++) tans2 += s[l][j];
    for (int l = 0; l < j; l++) tans3 += s[i][l];
    for (int l = j + 1; l < n; l++) tans4 += s[i][l];
    ans += min(min(tans1, tans2), min(tans3, tans4));
  }
  cout << ans << endl;
  return 0;
}
