#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b, int c) {
  if (b + c < a) {
    --a;
    return 1;
  }
  return 0;
}
int id[500][500];
int n, flag;
stack<pair<int, int> > stk;
int &access(int i) { return id[i / n][i % n]; }
int value(int i) { return id[i / n][i % n]; }
void update() {
  pair<int, int> top = stk.top();
  stk.pop();
  int i = top.first, j = top.second;
  if ((i + 1) % n != 0) {
    if (chmin(access(i + 1), access(i), j)) stk.push(pair<int, int>(i + 1, 1));
  }
  if (i % n != 0 && i != 0) {
    if (chmin(access(i - 1), access(i), j)) stk.push(pair<int, int>(i - 1, 1));
  }
  if ((i + n) < n * n) {
    if (chmin(access(i + n), access(i), j)) stk.push(pair<int, int>(i + n, 1));
  }
  if ((i - n) > 0) {
    if (chmin(access(i - n), access(i), j)) stk.push(pair<int, int>(i - n, 1));
  }
}
int main() {
  cin >> n;
  int a = 0;
  for (int i = 0; i < (n); ++i) {
    for (int j = 0; j < (n); ++j) {
      int tmp = min(a / n, a % n);
      int tmp2 = min((n * n - a - 1) / n, (n * n - a - 1) % n);
      id[i][j] = min(tmp, tmp2);
      a++;
    }
  }
  int res = 0;
  for (int i = 0; i < (n * n); ++i) {
    int tmp;
    cin >> tmp;
    tmp--;
    res += value(tmp);
    stk.push(pair<int, int>(tmp, 0));
    while (!stk.empty()) update();
  }
  cout << res << endl;
  return 0;
}
