#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 11;
const int MAXN = 1e5 + 5;
const int LG = 20;
const int INF = 1e9;
using ll = long long int;
using ull = unsigned long long int;
const ll MOD2 = 998244353;
const ll MOD1 = 1e9 + 7LL;
ll *inv;
template <typename T>
T power(T a, T b, T mod = MOD1) {
  T r = 1LL;
  while (b) {
    if (b & 1LL) {
      r = (r * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1LL;
  }
  return r;
}
void inversions() {
  inv = new ll[maxn];
  inv[0] = 0;
  for (int i = 1; i < maxn; ++i) {
    inv[i] = power((ll)i, MOD1 - 2LL);
  }
}
template <typename T>
T gcd(T a, T b) {
  while (b) {
    T t = b % a;
    a = b;
    b = t;
  }
  return a;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> order(n * n + 1);
  vector<pair<int, int>> pos(n * n + 1);
  for (int i = 1; i <= n * n; ++i) scanf("%d", &order[i]);
  int c = 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) pos[++c] = {i, j};
  int ans = 0;
  vector<vector<int>> vis(n, vector<int>(n, 0));
  function<bool(int, int)> safe = [&](int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < n;
  };
  function<bool(int, int)> isborder = [&](int x, int y) {
    return x == 0 || y == 0 || x == n - 1 || y == n - 1;
  };
  for (int i = 1; i < (int)order.size(); ++i) {
    int o = order[i];
    if (isborder(pos[o].first, pos[o].second)) {
      vis[pos[o].first][pos[o].second] = 2;
      continue;
    }
    vis[pos[o].first][pos[o].second] = 1;
    queue<pair<int, pair<int, int>>> q;
    q.push({0, pos[o]});
    set<pair<int, int>> cross;
    int res = -1;
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      if (isborder(p.second.first, p.second.second)) {
        res = res == -1 ? p.first : min(res, p.first);
        continue;
      }
      for (int i = -1; i <= 1; ++i) {
        for (int j = -1; j <= 1; ++j) {
          if (abs(i) == abs(j)) continue;
          int x = p.second.first + i;
          int y = p.second.second + j;
          if (!safe(x, y) || vis[x][y] == 1 || cross.count({x, y})) continue;
          cross.insert({x, y});
          q.push({p.first + (!vis[x][y]), {x, y}});
        }
      }
    }
    vis[pos[o].first][pos[o].second] = 2;
    ans += res;
  }
  printf("%d\n", ans);
  return 0;
}
