#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> pref(n + 1, vector<int>(n + 1, 1));
  vector<vector<int>> count(n + 1, vector<int>(n + 1, 0));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      count[i][j] = min(i - 1, min(n - i, min(j - 1, n - j)));
    }
  }
  vector<int> v(n * n);
  for (int i = 0; i < n * n; ++i) {
    cin >> v[i];
  }
  int ans = 0;
  for (int i = 0; i < n * n; ++i) {
    int curr = v[i];
    int row = (curr / n) + 1;
    if (curr % n == 0) {
      row--;
    }
    int col = (curr - n * (row - 1));
    if (pref[row][col] == 1) {
      ans += count[row][col];
      count[row][col] = 0;
      pref[row][col] = 0;
      int temp = 0;
      for (int j = 1; j <= n; ++j) {
        count[row][j] = min(temp, count[row][j]);
        if (pref[row][j] == 1) {
          temp++;
        }
      }
      temp = 0;
      for (int j = n; j >= 1; --j) {
        count[row][j] = min(temp, count[row][j]);
        if (pref[row][j] == 1) {
          temp++;
        }
      }
      temp = 0;
      for (int j = 1; j <= n; ++j) {
        count[j][col] = min(temp, count[j][col]);
        if (pref[j][col] == 1) {
          temp++;
        }
      }
      temp = 0;
      for (int j = n; j >= 1; --j) {
        count[j][col] = min(temp, count[j][col]);
        if (pref[j][col] == 1) {
          temp++;
        }
      }
    }
  }
  cout << ans << "\n";
}
