#include <bits/stdc++.h>
long long mod = 998244353;
const int inf = 0x3f3f3f3f;
using namespace std;
int mp[600][600];
int nx[4] = {0, 0, 1, -1};
int ny[4] = {1, -1, 0, 0};
int main() {
  int N;
  cin >> N;
  int sum = 0;
  for (int i = 0; i < N * N; i++) {
    int t;
    cin >> t;
    int x, y;
    y = t % N;
    if (y == 0) {
      x = t / N;
      y = N;
    } else
      x = t / N + 1;
    int minn = inf;
    mp[x][y] = 1;
    if (x == 1 || y == 1 || x == N || y == N) continue;
    for (int j = 0; j < 4; j++) {
      int ans = 0;
      int tx = x, ty = y;
      while (tx != 1 && ty != 1 && tx != N && ty != N) {
        tx += nx[j];
        ty += ny[j];
        if (mp[tx][ty] == 0) {
          ans++;
        }
      }
      minn = min(minn, ans);
    }
    sum += minn;
  }
  cout << sum << endl;
}
