#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
}
const int P = 1e9 + 7, N = 505;
int mul(int a, int b) { return 1ll * a * b % P; }
int add(int a, int b) {
  a += b;
  return a >= P ? a - P : a;
}
int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + P : a;
}
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int n, d[N][N], del[N][N], X[N * N], Y[N * N], ans;
void upd(int x, int y) {
  int dx[] = {-1, 0, 0, 1}, dy[] = {0, -1, 1, 0};
  for (int k = 0; k < (4); ++k) {
    int nx = x + dx[k], ny = y + dy[k];
    if (nx < 1 || nx > n || ny < 1 || ny > n) continue;
    if (d[nx][ny] > d[x][y] + (!del[x][y])) {
      d[nx][ny] = d[x][y] + (!del[x][y]);
      upd(nx, ny);
    }
  }
}
int main(int argc, char *argv[]) {
  read(n);
  for (int i = 1; i <= (n * n); ++i) {
    int u;
    read(u);
    X[i] = (u - 1) / n + 1, Y[i] = (u - 1) % n + 1;
  }
  for (int i = 1; i <= (n); ++i)
    for (int j = 1; j <= (n); ++j)
      d[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
  for (int i = 1; i <= (n * n); ++i) {
    d[X[i]][Y[i]] -= 1;
    ans += d[X[i]][Y[i]];
    del[X[i]][Y[i]] = 1;
    upd(X[i], Y[i]);
  }
  cout << ans << endl;
  return 0;
}
