#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<long long> data;
  UnionFind(long long size) : data(size, -1) {}
  bool union_(long long x, long long y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool find(long long x, long long y) { return root(x) == root(y); }
  long long root(long long x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  long long size(long long x) { return -data[root(x)]; }
};
long long N;
long long d[500][500];
bool inai[500][500];
UnionFind uf(501 * 501);
pair<long long, long long> fromP(long long p) {
  long long row = (p - 1) / N;
  long long col = (p - 1) % N;
  return {row, col};
}
long long toP(long long i, long long j) { return i * N + (j + 1); }
long long get_d(long long i, long long j) {
  long long dd = d[i][j];
  if (i + 1 < N && inai[i + 1][j]) {
    long long rr = uf.root(toP(i + 1, j));
    auto [ii, jj] = fromP(rr);
    dd = min(dd, d[ii][jj]);
  }
  if (j + 1 < N && inai[i][j + 1]) {
    long long rr = uf.root(toP(i, j + 1));
    auto [ii, jj] = fromP(rr);
    dd = min(dd, d[ii][jj]);
  }
  if (i - 1 >= 0 && inai[i - 1][j]) {
    long long rr = uf.root(toP(i - 1, j));
    auto [ii, jj] = fromP(rr);
    dd = min(dd, d[ii][jj]);
  }
  if (j - 1 >= 0 && inai[i][j - 1]) {
    long long rr = uf.root(toP(i, j - 1));
    auto [ii, jj] = fromP(rr);
    dd = min(dd, d[ii][jj]);
  }
  return dd;
}
void connect(long long i, long long j, long long f) {
  if (i + 1 < N && inai[i + 1][j]) uf.union_(toP(i, j), toP(i + 1, j));
  if (j + 1 < N && inai[i][j + 1]) uf.union_(toP(i, j), toP(i, j + 1));
  if (i - 1 >= 0 && inai[i - 1][j]) uf.union_(toP(i, j), toP(i - 1, j));
  if (j - 1 >= 0 && inai[i][j - 1]) uf.union_(toP(i, j), toP(i, j - 1));
  long long root = uf.root(toP(i, j));
  auto [ir, jr] = fromP(root);
  d[ir][jr] = min(f, d[ir][jr]);
}
signed main() {
  cin >> N;
  for (long long p = 1; p <= N * N; p++) {
    auto [i, j] = fromP(p);
    d[i][j] = min(min(i, abs(N - 1 - i)), min(j, abs(N - 1 - j)));
  }
  long long ans = 0;
  for (long long t = 0; t < N * N; t++) {
    long long p;
    cin >> p;
    auto [i, j] = fromP(p);
    long long f = get_d(i, j);
    ans += f;
    connect(i, j, f);
    inai[i][j] = true;
  }
  cout << ans << endl;
}
