#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& vec) {
  out << "[";
  for (int x : vec) out << x << ", ";
  out << "]";
  return out;
}
int N;
int P[500 * 500];
int cinema[500 * 500 + 1];
int q[500 * 500 + 1];
void compute() {
  int x = 0;
  vector<int> curr;
  vector<int> pross;
  vector<bool> inserito(N * N + 1, false);
  for (int i = 1; i <= N; i++)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = N * N - N + 1; i <= N * N; i++)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = N + 1; i < N * N - N; i += N)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = 2 * N; i < N * N; i += N)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int p : curr) inserito[p] = true;
  for (int p : pross) inserito[p] = true;
  for (int i = 0; i < N * N; i++) {
    if (curr.empty()) {
      x++;
      curr = pross;
      pross.clear();
    }
    int p = curr.back();
    curr.pop_back();
    q[p] = x;
    if (p > N and not inserito[p - N]) {
      inserito[p - N] = true;
      if (cinema[p - N])
        pross.push_back(p - N);
      else
        curr.push_back(p - N);
    }
    if (p <= (N - 1) * N and not inserito[p + N]) {
      inserito[p + N] = true;
      if (cinema[p + N])
        pross.push_back(p + N);
      else
        curr.push_back(p + N);
    }
    if ((p % N != 0) and not inserito[p + 1]) {
      inserito[p + 1] = true;
      if (cinema[p + 1])
        pross.push_back(p + 1);
      else
        curr.push_back(p + 1);
    }
    if ((p % N != 1) and not inserito[p - 1]) {
      inserito[p - 1] = true;
      if (cinema[p - 1])
        pross.push_back(p - 1);
      else
        curr.push_back(p - 1);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
    cinema[i] = 1;
  }
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    cinema[P[i]] = 0;
    compute();
    ans += q[P[i]];
  }
  cout << ans << '\n';
  return 0;
}
