#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30) - 1;
int x[4] = {-1, 0, 1, 0};
int y[4] = {0, -1, 0, 1};
int N;
vector<int> P;
bool b[501][501];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  P.resize(N * N);
  for (int i = 0; i < N * N; i++) {
    int tmp;
    cin >> tmp;
    tmp--;
    P[i] = tmp;
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int row = P[i] / N;
    int col = P[i] % N;
    int m = INF;
    for (int i = 0; i < 4; i++) {
      int sum = 0;
      int r = row;
      int c = col;
      r += x[i];
      c += y[i];
      while (0 <= r && r < N && 0 <= c && c < N) {
        if (!b[r][c]) {
          sum++;
        }
        r += x[i];
        c += y[i];
      }
      m = min(m, sum);
    }
    ans += m;
    b[row][col] = true;
  }
  cout << ans << endl;
  return 0;
}
