#include <bits/stdc++.h>
using namespace std;
const int dy[] = {0, 0, 1, -1};
const int dx[] = {-1, 1, 0, 0};
int main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  int ans = 0;
  vector<vector<int> > v(n, vector<int>(n, -1));
  for (int i = 0; i < n * n; ++i) {
    int p;
    cin >> p;
    --p;
    int sy = p / n;
    int sx = p % n;
    deque<tuple<int, int, int> > dq;
    dq.push_back(make_tuple(sy, sx, 0));
    for (;;) {
      auto [y, x, c] = dq.front();
      dq.pop_front();
      bool ok = false;
      for (int j = 0; j < 4; ++j) {
        int y2 = y + dy[j];
        int x2 = x + dx[j];
        if (!(0 <= y2 && y2 < n && 0 <= x2 && x2 < n)) {
          ans += c;
          ok = true;
          break;
        }
        if (v[y2][x2] == INT_MAX) {
          dq.push_front(make_tuple(y2, x2, c));
        } else if (v[y2][x2] < i) {
          dq.push_back(make_tuple(y2, x2, c + 1));
          v[y][x] = i;
        }
      }
      if (ok) break;
    }
    v[sy][sx] = INT_MAX;
  }
  cout << ans << endl;
  return 0;
}
