#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using MaxHeap = priority_queue<T>;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
const double PI = 3.141592653589793238462643383279;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 55;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll dx[] = {-1, 1, 0, 0};
ll dy[] = {0, 0, 1, -1};
ll table[500][500];
bool empty[500][500];
ll N;
void f(ll y, ll x) {
  for (ll i = 0; i < 4; i++) {
    ll yy = y + dy[i];
    ll xx = x + dx[i];
    if (yy < 0 || xx < 0 || yy >= N || xx >= N) continue;
    ll val = table[y][x] + (empty[y][x] ? 0 : 1);
    if (table[yy][xx] > val) {
      table[yy][xx] = val;
      f(yy, xx);
    }
  }
}
signed main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  vector<ll> A(N * N);
  for (ll i = 0; i < N * N; i++) cin >> A[i];
  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < N; j++) {
      table[i][j] = min({i, j, N - 1 - i, N - 1 - j});
    }
  }
  ll ret = 0;
  for (ll i = 0; i < N * N; i++) {
    ll y = (A[i] - 1) / N;
    ll x = (A[i] - 1) % N;
    ret += table[y][x];
    empty[y][x] = true;
    f(y, x);
  }
  cout << ret << endl;
  return 0;
}
