#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<vector<bool>> seen(N, vector<bool>(N, true));
  vector<pair<int, int>> p(N);
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int P;
    cin >> P, P--;
    int sum = N, cnt = 0;
    for (int j = 0; j < P % N; j++)
      if (seen[P / N][j]) cnt++;
    sum = min(sum, cnt), cnt = 0;
    for (int j = 0; j < P / N; j++)
      if (seen[j][P % N]) cnt++;
    sum = min(sum, cnt), cnt = 0;
    for (int j = N - 1; j >= P / N + 1; j--)
      if (seen[j][P % N]) cnt++;
    sum = min(sum, cnt), cnt = 0;
    for (int j = N - 1; j >= P % N + 1; j--)
      if (seen[P / N][j]) cnt++;
    sum = min(sum, cnt), cnt = 0;
    ans += sum;
    seen[P / N][P % N] = false;
  }
  cout << ans << endl;
  return 0;
}
