#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<bool> mp(n * n, true);
  const int dx[4] = {1, -1, 0, 0};
  const int dy[4] = {0, 0, 1, -1};
  using P = pair<int, int>;
  int ans = 0;
  for (int i = 0; i < (n * n); ++i) {
    int p;
    cin >> p;
    p--;
    priority_queue<P, vector<P>, greater<P>> pq;
    mp[p] = false;
    pq.emplace(make_pair(0, p));
    const int INF = INT_MAX;
    vector<int> min_k_mp(n * n, INF);
    min_k_mp[p] = 0;
    int min_k = INF;
    while (!pq.empty()) {
      auto now = pq.top();
      pq.pop();
      int x = now.second % n;
      int y = now.second / n;
      int k = now.first;
      if (min_k <= k) break;
      for (int i = 0; i < (4); ++i) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || nx >= n || ny < 0 | ny >= n) {
          chmin(min_k, k);
          continue;
        }
        int nk = k + mp[ny * n + nx];
        if (min_k_mp[ny * n + nx] <= nk) continue;
        min_k_mp[ny * n + nx] = nk;
        pq.emplace(make_pair(nk, ny * n + nx));
      }
    }
    ans += min_k;
  }
  cout << ans << endl;
}
