#include <bits/stdc++.h>
using namespace std;
map<int, int> ct;
map<int, int> ct2;
int n, m, l;
int a, b, c;
string second[100005];
string str;
string ans;
int lmin(int i, int j, int N, vector<vector<int> >& flag, int k) {
  int len;
  if (i == 0 || i == N - 1 || j == 0 || j == N - 1) {
    return 0;
  }
  if (k == 0) {
    len = min(lmin(i - 1, j, N, flag, 1) + flag.at(i - 1).at(j),
              min(lmin(i + 1, j, N, flag, 2) + flag.at(i + 1).at(j),
                  min(lmin(i, j - 1, N, flag, 3) + flag.at(i).at(j - 1),
                      lmin(i, j + 1, N, flag, 4) + flag.at(i).at(j + 1))));
  } else if (k == 1) {
    len = min(lmin(i - 1, j, N, flag, 1) + flag.at(i - 1).at(j),
              min(lmin(i, j - 1, N, flag, 3) + flag.at(i).at(j - 1),
                  lmin(i, j + 1, N, flag, 4) + flag.at(i).at(j + 1)));
  } else if (k == 2) {
    len = min(lmin(i + 1, j, N, flag, 2) + flag.at(i + 1).at(j),
              min(lmin(i, j - 1, N, flag, 3) + flag.at(i).at(j - 1),
                  lmin(i, j + 1, N, flag, 4) + flag.at(i).at(j + 1)));
  } else if (k == 3) {
    len = min(lmin(i - 1, j, N, flag, 1) + flag.at(i - 1).at(j),
              min(lmin(i + 1, j, N, flag, 2) + flag.at(i + 1).at(j),
                  lmin(i, j - 1, N, flag, 3) + flag.at(i).at(j - 1)));
  } else {
    len = min(lmin(i - 1, j, N, flag, 1) + flag.at(i - 1).at(j),
              min(lmin(i + 1, j, N, flag, 2) + flag.at(i + 1).at(j),
                  lmin(i, j + 1, N, flag, 4) + flag.at(i).at(j + 1)));
  }
  return len;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  long long sum = 0;
  cin >> N;
  vector<int> P(N * N);
  vector<vector<int> > A(N, vector<int>(N));
  vector<vector<int> > flag(N, vector<int>(N));
  for (int i = 0; i < (signed)(N * N); i++) {
    cin >> P.at(i);
  }
  for (int i = 0; i < (signed)(N); i++) {
    for (int j = 0; j < (signed)(N); j++) {
      flag.at(i).at(j) = 1;
    }
  }
  for (int i = 0; i < (signed)(N * N); i++) {
    int x, y;
    x = P.at(i) / N;
    y = P.at(i) % N - 1;
    if (P.at(i) % N == 0) {
      x = x - 1;
      y = y + N;
    }
    sum += lmin(x, y, N, flag, 0);
    flag.at(x).at(y) = 0;
  }
  cout << sum << endl;
  return 0;
}
