#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> dist(n, vector<int>(n));
  vector<vector<int>> exist(n, vector<int>(n));
  vector<int> p(n * n);
  for (int i = 0; i < (n * n); ++i) {
    cin >> p[i];
  }
  for (int i = 0; i < (n); ++i) {
    for (int j = 0; j < (n); ++j) {
      exist[i][j] = 1;
    }
  }
  int row;
  int col;
  int center;
  int ans = 0;
  for (int i = 0; i < (n * n); ++i) {
    row = (p[i] - 1) / n;
    col = (p[i] - 1) % n;
    center = n / 2;
    int up = 0, down = 0, right = 0, left = 0;
    for (int row_i = 0; row_i < n; row_i++) {
      if (row_i < row) {
        up += exist[row_i][col];
      } else if (row_i > row) {
        down += exist[row_i][col];
      }
    }
    for (int col_i = 0; col_i < n; col_i++) {
      if (col_i < col) {
        left += exist[row][col_i];
      } else if (col_i > col) {
        right += exist[row][col_i];
      }
    }
    ans += min(min(up, down), min(left, right));
    exist[row][col] = 0;
  }
  cout << ans << endl;
  return 0;
}
