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