#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  bool a[n][n];
  memset(a, true, sizeof(a));
  long long int ans = 0;
  for (long long int i = 0; i < n * n; i++) {
    long long int t, temp = 1e9;
    cin >> t;
    t--;
    long long int x = t / n, y = t % n;
    if (x == 0 || y == 0 || x == n - 1 || y == n - 1)
      temp = 0;
    else {
      long long int k = x;
      for (long long int i = x - 1; i >= 0; i--)
        if (!a[i][y]) k--;
      temp = min(k, temp);
      k = n - 1 - x;
      for (long long int i = x + 1; i < n; i++)
        if (!a[i][y]) k--;
      temp = min(k, temp);
      k = y;
      for (long long int i = y - 1; i >= 0; i--)
        if (!a[x][i]) k--;
      temp = min(k, temp);
      k = n - 1 - y;
      for (long long int i = y + 1; i < n; i++)
        if (!a[x][i]) k--;
      temp = min(k, temp);
    }
    if (temp != 1e9) ans += temp;
    a[x][y] = false;
  }
  cout << ans << endl;
  return 0;
}
