#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
void print() {}
template <class H, class... T>
void print(H&& h, T&&... t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}
ll cnt[510][510] = {};
bool check[510][510] = {};
ll N;
void debug() {
  for (ll i = (0); i < (N); i++) {
    for (ll j = (0); j < (N); j++) {
      cout << cnt[i][j] << " ";
    }
    cout << endl;
  }
  print("===");
  for (ll i = (0); i < (N); i++) {
    for (ll j = (0); j < (N); j++) {
      cout << check[i][j] << " ";
    }
    cout << endl;
  }
  print("===");
}
void solve() {
  cin >> N;
  ll P[250010];
  for (ll i = (0); i < (N * N); i++) cin >> P[i];
  ll pos[510][510] = {};
  for (ll i = (0); i < (N); i++) {
    for (ll j = (0); j < (N); j++) {
      pos[i][j] = i * N + j;
      check[i][j] = true;
    }
  }
  for (ll i = (0); i < (N); i++) {
    for (ll j = (0); j < (N); j++) {
      ll diff = min(i, N - 1 - i);
      diff = min(diff, min(j, N - 1 - j));
      cnt[i][j] = diff;
    }
  }
  ll ans = 0;
  for (ll i = (0); i < (N * N); i++) {
    ll ii = (P[i] - 1) / N;
    ll jj = (P[i] - 1) % N;
    ans += cnt[ii][jj];
    ll val = 0;
    for (ll k = (0); k < (ii + 1); k++) {
      if (check[k][jj]) val++;
    }
    for (ll k = (ii + 1); k < (N); k++) {
      if (cnt[k][jj] == val) {
        cnt[k][jj]--;
      }
      if (check[k][jj]) val++;
    }
    val = 0;
    for (ll k = (ii); k < (N); k++) {
      if (check[k][jj]) val++;
    }
    for (ll k = (ii - 1); k >= (0); k--) {
      if (cnt[k][jj] == val) {
        cnt[k][jj]--;
      }
      if (check[k][jj]) val++;
    }
    val = 0;
    for (ll k = (0); k < (jj + 1); k++) {
      if (check[ii][k]) val++;
    }
    for (ll k = (jj + 1); k < (N); k++) {
      if (cnt[ii][k] == val) {
        cnt[ii][k]--;
      }
      if (check[ii][k]) val++;
    }
    val = 0;
    for (ll k = (jj); k < (N); k++) {
      if (check[ii][k]) val++;
    }
    for (ll k = (jj - 1); k >= (0); k--) {
      if (cnt[ii][k] == val) {
        cnt[ii][k]--;
      }
      if (check[ii][k]) val++;
    }
    check[ii][jj] = false;
  }
  print(ans);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
}
