#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  int res = 0;
  vector<vector<bool>> T(N, vector<bool>(N, true));
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;
    int y = p / N, x = p % N;
    int left = 0, right = 0, up = 0, down = 0;
    for (int j = x + 1; j < N; j++) {
      if (T[y][j]) right++;
    }
    for (int j = x - 1; j >= 0; j--) {
      if (T[y][j]) left++;
    }
    for (int j = y + 1; j < N; j++) {
      if (T[j][x]) up++;
    }
    for (int j = x - 1; j >= 0; j--) {
      if (T[j][x]) down++;
    }
    res += min({left, right, up, down});
    T[y][x] = false;
  }
  cout << res << '\n';
  return 0;
}
