#include <bits/stdc++.h>
using namespace std;
const bool ready = []() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);
  return true;
}();
const double PI = acos(-1);
using ll = long long;
using pii = pair<ll, ll>;
using pdd = pair<double, double>;
using vd = vector<double>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vs = vector<string>;
void solve() {
  ll n;
  cin >> n;
  ;
  vi p(n * n);
  for (ll i = 0; i < ll(n * n); i++) {
    cin >> p[i];
  };
  vvi dp(n, vi(n));
  for (ll i = 0; i < n; i++)
    for (ll j = 0; j < n; j++)
      dp[i][j] = 1 + min(i, min(j, min(n - 1 - i, n - 1 - j)));
  vvi notvis(n, vi(n, 1));
  const vector<pii> d = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  ll ans = 0;
  for (ll i = 0; i < n * n; i++) {
    ll r = (p[i] - 1) / n;
    ll c = (p[i] - 1) % n;
    ll mi = 1e9;
    for (auto d1 : d) {
      ll rr = r + d1.first;
      ll cc = c + d1.second;
      if (rr >= 0 && rr < n && cc >= 0 && cc < n)
        mi = min(mi, dp[rr][cc]);
      else
        mi = 0;
    }
    ans += mi;
    dp[r][c] = mi;
    notvis[r][c] = 0;
    queue<pii> q;
    q.emplace(r, c);
    while (q.size()) {
      auto [r0, c0] = q.front();
      q.pop();
      for (auto d1 : d) {
        ll rr = r0 + d1.first;
        ll cc = c0 + d1.second;
        if (rr >= 0 && rr < n && cc >= 0 && cc < n &&
            dp[rr][cc] > dp[r0][c0] + notvis[rr][cc]) {
          dp[rr][cc] = dp[r][c] + notvis[rr][cc];
          q.emplace(rr, cc);
        }
      }
    }
  }
  cout << ans << endl;
}
signed main() { solve(); }
