#include <bits/stdc++.h>
using namespace std;
void out_seat(vector<vector<int>> s, vector<vector<bool>> l) {
  for (int i = 0; i < (int)(s.size()); i++) {
    for (int j = 0; j < (int)(s.at(i).size()); j++) {
      if (l.at(i).at(j))
        cout << "x";
      else
        cout << s.at(i).at(j);
    }
    cout << endl;
  }
}
int main() {
  int n;
  cin >> n;
  vector<int> x(n * n);
  for (int i = 0; i < (int)(n * n); i++) cin >> x.at(i);
  vector<vector<int>> seat(n);
  vector<vector<bool>> lvfl(n);
  for (int i = 0; i < (int)(n); i++) {
    for (int j = 0; j < (int)(n); j++) {
      int d = min(min(min(i, j), n - i - 1), n - j - 1);
      seat.at(i).push_back(d);
      lvfl.at(i).push_back(false);
    }
  }
  long long tot;
  queue<pair<int, int>> q;
  for (int cur : x) {
    q.push({(cur - 1) % n, (cur - 1) / n});
    lvfl.at((cur - 1) / n).at((cur - 1) % n) = true;
    tot += seat.at((cur - 1) / n).at((cur - 1) % n);
    while (q.size()) {
      auto p = q.front();
      q.pop();
      int cx = p.first, cy = p.second;
      pair<int, int> plist[] = {
          {cx - 1, cy}, {cx, cy - 1}, {cx + 1, cy}, {cx, cy + 1}};
      int ofs = lvfl.at(cy).at(cx) ? 0 : 1;
      for (auto pp : plist) {
        if (pp.first < 0) continue;
        if (pp.second < 0) continue;
        if (pp.first >= n) continue;
        if (pp.second >= n) continue;
        int tx = pp.first, ty = pp.second;
        if (seat.at(cy).at(cx) + ofs < seat.at(ty).at(tx)) {
          seat.at(ty).at(tx) = seat.at(cy).at(cx) + ofs;
          q.push(pp);
        }
      }
    }
  }
  cout << tot << endl;
  return 0;
}
