#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> mp(n, vector<int>(n));
  vector<pii> jun;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    mp.at(p / n).at(p % n) = i;
    jun.emplace_back(p / n, p % n);
  }
  ll ans = 0;
  for (auto &st : jun) {
    vector<vector<int>> cmp(n, vector<int>(n, -1));
    cmp.at(st.first).at(st.second) = 0;
    queue<pii> q;
    q.push(st);
    int num = mp.at(st.first).at(st.second);
    int mcost = static_cast<int>(500 * 500 + 5);
    while (!q.empty()) {
      if (mcost == 0) break;
      pii cp = q.front();
      q.pop();
      if (cp.first == 0 || cp.first == n - 1 || cp.second == 0 ||
          cp.second == n - 1) {
        mcost = min(mcost, cmp.at(cp.first).at(cp.second));
        continue;
      }
      for (int i = 0, j = -1, k = 0; k < 4; j += i, i = j - i, j = i - j, k++) {
        int ni = i + cp.first;
        int nj = j + cp.second;
        int nc =
            cmp.at(cp.first).at(cp.second) + (num < mp.at(ni).at(nj) ? 1 : 0);
        if (cmp.at(ni).at(nj) == -1 || cmp.at(ni).at(nj) > nc) {
          cmp.at(ni).at(nj) = nc;
          if (mcost > nc) {
            q.push(make_pair(ni, nj));
          }
        }
      }
    }
    ans += mcost;
  }
  cout << ans << '\n';
  return 0;
}
