#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1000000007;
long long INF = 4001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T>& vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}
using Graph = vector<vector<long long>>;
long long dx[4]{0, -1, 0, 1};
long long dy[4]{-1, 0, 1, 0};
long long N;
long long P[250010];
char field[510][510];
long long now[510][510];
long long ans = 0;
vector<pair<long long, long long>> PS[250010];
struct UnionFind {
  vector<int> par;
  vector<long long> S;
  UnionFind(int N) : par(N), S(N) {
    for (long long i = (long long)0; i < (long long)N; i++) {
      par[i] = i;
      S[i] = 1;
    }
  }
  int root(int x) {
    while (x != par[x]) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (!same(x, y)) {
      S[root(x)] += S[root(y)];
      S[root(y)] = S[root(x)];
    }
    if (rx == ry) return;
    if (S[x] >= S[y]) {
      par[ry] = rx;
    } else {
      par[rx] = ry;
    }
  }
  long long SIZE(int x) { return S[root(x)]; }
};
UnionFind UF(250000);
long long F(long long y, long long x) { return N * y + x; }
bool In(long long y, long long x) { return 0 <= y && y < N && 0 <= x && x < N; }
int main() {
  cin >> N;
  for (long long i = (long long)0; i < (long long)N * N; i++) {
    cin >> P[i];
    P[i]--;
  }
  for (long long i = (long long)0; i < (long long)N; i++) {
    for (long long j = (long long)0; j < (long long)N; j++) {
      now[i][j] = min({i, j, N - i - 1, N - j - 1});
    }
  }
  for (long long i = (long long)0; i < (long long)N; i++)
    for (long long j = (long long)0; j < (long long)N; j++) field[i][j] = '#';
  for (long long i = (long long)0; i < (long long)N * N; i++) {
    long long y, x;
    y = P[i] / N;
    x = P[i] % N;
    ans += now[y][x];
    field[y][x] = '.';
    for (long long j = (long long)0; j < (long long)4; j++) {
      long long ny = y + dy[j], nx = x + dx[j];
      if (In(ny, nx)) {
        if (field[ny][nx] == '.') UF.unite(F(ny, nx), F(y, x));
      }
    }
    for (long long j = (long long)0; j < (long long)4; j++) {
      long long ny = y + dy[j], nx = x + dx[j];
      if (In(ny, nx)) {
        PS[UF.par[F(y, x)]].emplace_back(y, x);
      }
    }
    long long a = UF.par[F(y, x)];
    for (long long j = (long long)0; j < (long long)PS[a].size(); j++) {
      long long ny = PS[a][j].first, nx = PS[a][j].second;
      for (long long k = (long long)0; k < (long long)4; k++) {
        long long nny = ny + dy[k], nnx = nx + dx[k];
        if (In(nny, nnx)) {
          if (field[nny][nnx] == '#') {
            now[nny][nnx] = min(now[nny][nnx], now[y][x]);
          }
        }
      }
    }
  }
  cout << ans << endl;
}
