#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, i, j, k, l, ans = 0, row, col, cnt = 0, cnt1;
  cin >> n;
  int visit[n][n];
  vector<int> v(n * n);
  for (int i = (0); i < (n); i++) {
    for (int j = (0); j < (n); j++) {
      visit[i][j] = 0;
    }
  }
  for (int i = (0); i < (n * n); i++) cin >> v[i];
  for (int i = (0); i < (n * n); i++) {
    if (v[i] % n == 1 || v[i] % n == 0 || v[i] / n == 0 || v[i] / n == n - 1) {
      if (v[i] % n == 0)
        visit[v[i] / n - 1][n - 1] = 1;
      else if (v[i] % n == 1)
        visit[v[i] / n][0] = 1;
      else if (v[i] / n == 0)
        visit[0][v[i] - 1] = 1;
      else
        visit[n - 1][(v[i] % n) - 1] = 1;
    } else {
      row = v[i] / n;
      col = (v[i] % n) - 1;
      cnt1 = (int)INT_MAX;
      cnt = 0;
      for (int j = 0; j < row; j++)
        if (visit[j][col] == 0) cnt++;
      cnt1 = min(cnt, cnt1);
      cnt = 0;
      for (int j = row + 1; j < n; j++)
        if (visit[j][col] == 0) cnt++;
      cnt1 = min(cnt, cnt1);
      cnt = 0;
      for (int j = col + 1; j < n; j++)
        if (visit[row][j] == 0) cnt++;
      cnt1 = min(cnt, cnt1);
      cnt = 0;
      for (int j = 0; j < col; j++)
        if (visit[row][j] == 0) cnt++;
      cnt1 = min(cnt, cnt1);
      ans += cnt1;
      visit[row][col] = 1;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL);
  int t = 1;
  while (t--) solve();
  return 0;
}
