#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long mod = 1e9 + 7;
const long long INF = 1LL << 60;
const long long m = 1LL << 32;
int main() {
  long long n;
  cin >> n;
  bool s[n][n];
  memset(s, true, sizeof(s));
  long long score[n][n][4];
  for (long long i = 0; i < (n); i++) {
    for (long long j = 0; j < (n); j++) {
      score[i][j][0] = i;
      score[i][j][1] = j;
      score[i][j][2] = n - 1 - j;
      score[i][j][3] = n - 1 - i;
    }
  }
  long long dy[4] = {-1, 0, 0, 1}, dx[4] = {0, -1, 1, 0};
  long long ans = 0;
  for (long long i = 0; i < (n * n); i++) {
    long long a;
    cin >> a;
    a--;
    long long y = a / n, x = a % n;
    s[y][x] = false;
    for (long long i = y - 1; i >= (0); i--) {
      score[i][x][3]--;
    }
    for (long long j = x - 1; j >= (0); j--) {
      score[y][j][2]--;
    }
    for (long long j = x + 1; j < (n); j++) {
      score[y][j][1]--;
    }
    for (long long i = y + 1; i < (n); i++) {
      score[i][x][0]--;
    }
    long long minScore = INF;
    for (long long j = 0; j < (4); j++) {
      long long nx = x + dx[j], ny = y + dy[j];
      if ((0 <= nx && nx < n) && (0 <= ny && ny < n)) {
        for (long long k = 0; k < (4); k++) {
          chmin(minScore, max((long long)0, score[ny][nx][k]) + (s[ny][nx]));
        }
      } else
        chmin(minScore, (long long)0);
    }
    ans += max((long long)0, minScore);
  }
  cout << ans << endl;
  return 0;
}
