#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
class UnionFind {
 public:
  std::vector<int> data;
  UnionFind(int n) { data.resize(n, -1); }
  int find(int x) {
    if (data[x] < 0) {
      return x;
    } else {
      int r = find(data[x]);
      data[x] = r;
      return r;
    }
  }
  void unite(int x, int y) {
    int r = find(x);
    int s = find(y);
    if (r != s) {
      data[r] += data[s];
      data[s] = r;
    }
  }
  int size(int x) {
    int r = find(x);
    return -data[r];
  }
};
ll solve() {
  ll N;
  cin >> N;
  vector<ll> P(N * N);
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
  }
  ll M = 2 * N + 1;
  auto m2ij = [&M](ll m) { return pii(m / M, m % M); };
  auto n2ij = [&N](ll n) { return pii(n / N, n % N); };
  auto ij2m = [&M](ll i, ll j) { return i * M + j; };
  auto ij2n = [&N](ll i, ll j) { return i * N + j; };
  UnionFind uf(M * M);
  vector<int> w(M * M);
  for (int k = 0; k < N; k++) {
    for (int i = k + 1; i < M - k; i += 2) {
      w[ij2m(k, i)] = (k + 1) / 2;
      w[ij2m(M - 1 - k, i)] = (k + 1) / 2;
      w[ij2m(i, k)] = (k + 1) / 2;
      w[ij2m(i, M - 1 - k)] = (k + 1) / 2;
    }
  }
  vector<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    auto nij = n2ij(P[i] - 1);
    int x0 = nij.first * 2 + 1, y0 = nij.second * 2 + 1;
    int h = N;
    for (int ii = 0; ii < 4; ii++) {
      int x = x0 + dx[ii];
      int y = y0 + dy[ii];
      int k = uf.find(ij2m(x, y));
      h = min(h, w[k]);
    }
    ans += h;
    ll p0 = ij2m(x0 + dx[0], y0 + dy[0]);
    w[uf.find(p0)] = h;
    for (int ii = 1; ii < 4; ii++) {
      int x = x0 + dx[ii];
      int y = y0 + dy[ii];
      ll p = ij2m(x, y);
      uf.unite(p0, p);
      w[uf.find(p)] = h;
    }
  }
  return ans;
}
int main() {
  auto ans = solve();
  cout << ans << "\n";
  return 0;
}
