#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  vector<vector<int>> mp(n, vector<int>(n));
  vector<pii> derujun;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    mp.at(p / n).at(p % n) = i;
    derujun.push_back(make_pair(p / n, p % n));
  }
  ll ans = 0;
  vector<pii> motion{make_pair(1, 0), make_pair(-1, 0), make_pair(0, 1),
                     make_pair(0, -1)};
  for (auto &st : derujun) {
    vector<vector<int>> cmp(n, vector<int>(n, -1));
    cmp.at(st.first).at(st.second) = 0;
    queue<pii> q;
    q.push(st);
    int mincost = -1;
    while (!q.empty()) {
      pii cp = q.front();
      q.pop();
      if (cp.first == 0 || cp.first == n - 1 || cp.second == 0 ||
          cp.second == n - 1) {
        if (mincost == -1)
          mincost = cmp.at(cp.first).at(cp.second);
        else
          mincost = min(mincost, cmp.at(cp.first).at(cp.second));
        continue;
      }
      for (auto &m : motion) {
        int ni = m.first + cp.first;
        int nj = m.second + cp.second;
        if (ni < 0 || n <= ni || nj < 0 || n <= nj) continue;
        if (cmp.at(ni).at(nj) != -1) {
          if (!chmin(
                  cmp.at(ni).at(nj),
                  cmp.at(cp.first).at(cp.second) +
                      (mp.at(ni).at(nj) > mp.at(cp.first).at(cp.second) ? 1
                                                                        : 0)))
            continue;
        } else {
          cmp.at(ni).at(nj) =
              cmp.at(cp.first).at(cp.second) +
              (mp.at(ni).at(nj) > mp.at(st.first).at(st.second) ? 1 : 0);
        }
        if (mincost == -1 || mincost > cmp.at(ni).at(nj)) {
          q.push(make_pair(ni, nj));
        }
      }
    }
    if (mincost != -1) ans += mincost;
  }
  cout << ans << '\n';
  return 0;
}
