#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,
         vector<vector<int> > history, int second, int A) {
  int len;
  int m1, m2, m3, m4;
  int f1, f2, f3;
  history.at(i).at(j) = 0;
  if (i == 0 || i == N - 1 || j == 0 || j == N - 1) {
    return 0;
  } else if (second >= A) {
    return second;
  }
  if (history.at(i).at(j - 1) == 0)
    m1 = 10000;
  else {
    second += flag.at(i).at(j - 1);
    f1 = history.at(i).at(j + 1);
    f2 = history.at(i - 1).at(j);
    f3 = history.at(i + 1).at(j);
    history.at(i).at(j + 1) = 0;
    history.at(i - 1).at(j) = 0;
    history.at(i + 1).at(j) = 0;
    m1 = lmin(i, j - 1, N, flag, history, second, A) + flag.at(i).at(j - 1);
    history.at(i).at(j + 1) = f1;
    history.at(i - 1).at(j) = f2;
    history.at(i + 1).at(j) = f3;
  }
  if (history.at(i).at(j + 1) == 0)
    m2 = 10000;
  else {
    second += flag.at(i).at(j + 1);
    f1 = history.at(i).at(j - 1);
    f2 = history.at(i - 1).at(j);
    f3 = history.at(i + 1).at(j);
    history.at(i).at(j - 1) = 0;
    history.at(i - 1).at(j) = 0;
    history.at(i + 1).at(j) = 0;
    m2 = lmin(i, j + 1, N, flag, history, second, A) + flag.at(i).at(j + 1);
    history.at(i).at(j - 1) = f1;
    history.at(i - 1).at(j) = f2;
    history.at(i + 1).at(j) = f3;
  }
  if (history.at(i - 1).at(j) == 0)
    m3 = 10000;
  else {
    second += flag.at(i - 1).at(j);
    f1 = history.at(i).at(j - 1);
    f2 = history.at(i).at(j + 1);
    f3 = history.at(i + 1).at(j);
    history.at(i).at(j - 1) = 0;
    history.at(i).at(j + 1) = 0;
    history.at(i + 1).at(j) = 0;
    m3 = lmin(i - 1, j, N, flag, history, second, A) + flag.at(i - 1).at(j);
    history.at(i).at(j - 1) = f1;
    history.at(i).at(j + 1) = f2;
    history.at(i + 1).at(j) = f3;
  }
  if (history.at(i + 1).at(j) == 0)
    m4 = 10000;
  else {
    second += flag.at(i + 1).at(j);
    f1 = history.at(i).at(j - 1);
    f2 = history.at(i).at(j + 1);
    f3 = history.at(i - 1).at(j);
    history.at(i).at(j - 1) = 0;
    history.at(i).at(j + 1) = 0;
    history.at(i - 1).at(j) = 0;
    m4 = lmin(i + 1, j, N, flag, history, second, A) + flag.at(i + 1).at(j);
    history.at(i).at(j - 1) = f1;
    history.at(i).at(j + 1) = f2;
    history.at(i - 1).at(j) = f3;
  }
  len = min(min(m1, m2), min(m3, m4));
  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));
  vector<vector<int> > history(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;
      history.at(i).at(j) = 1;
    }
  }
  if (N % 2 == 0) {
    for (int i = 0; i < (signed)(N / 2); i++) {
      for (int j = 0; j < (signed)(N / 2); j++) {
        A.at(i).at(j) = min(i, j);
        A.at(N - i - 1).at(j) = min(i, j);
        A.at(N - i - 1).at(N - j - 1) = min(i, j);
        A.at(i).at(N - j - 1) = min(i, j);
      }
    }
  } else {
    for (int i = 0; i < (signed)(N / 2); i++) {
      A.at(N / 2).at(i) = i;
      A.at(i).at(N / 2) = i;
      A.at(i).at(N - i - 1) = i;
      A.at(N - i - 1).at(i) = i;
      for (int j = 0; j < (signed)(N / 2); j++) {
        A.at(i).at(j) = min(i, j);
        A.at(N - i - 1).at(j) = min(i, j);
        A.at(N - i - 1).at(N - j - 1) = min(i, j);
        A.at(i).at(N - j - 1) = min(i, j);
      }
    }
    A.at(N / 2).at(N / 2) = N / 2;
  }
  for (int i = 0; i < (signed)(N * N); i++) {
    int x, y, second;
    second = 0;
    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, history, second, A.at(x).at(y));
    flag.at(x).at(y) = 0;
  }
  cout << sum << endl;
  return 0;
}
