#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 dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int N;
int bfs(int x, int y, vector<vector<int> > &A, vector<vector<int> > flag) {
  queue<pair<int, int> > que;
  que.push(pair<int, int>(x, y));
  while (que.size()) {
    pair<int, int> p = que.front();
    que.pop();
    for (int i = 0; i < (signed)(4); i++) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (0 <= nx && nx < N && 0 <= ny && ny < N) {
        if (A.at(p.first).at(p.second) + flag.at(p.first).at(p.second) <
            A.at(nx).at(ny)) {
          A.at(nx).at(ny) =
              A.at(p.first).at(p.second) + flag.at(p.first).at(p.second);
          que.push(pair<int, int>(nx, ny));
        }
      }
    }
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  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); i++) {
    for (int j = 0; j < (signed)(N); j++) {
      flag.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(N - i - 1).at(N / 2) = i;
      A.at(N / 2).at(N - i - 1) = 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;
    cin >> P.at(i);
    x = P.at(i) / N;
    y = P.at(i) % N - 1;
    if (y == -1) {
      x = x - 1;
      y = y + N;
    }
    sum += A.at(x).at(y);
    flag.at(x).at(y) = 0;
    bfs(x, y, A, flag);
  }
  cout << sum << endl;
  return 0;
}
