#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
i64 dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int main() {
  i64 N;
  cin >> N;
  vector<i64> P(N * N);
  for (i64 i = 0; i < N * N; i++) cin >> P[i];
  i64 ans = 0;
  vector<vector<bool>> use(N, vector<bool>(N, true));
  for (i64 i = 0; i < N * N; i++) {
    i64 x = (P[i] - 1) / N, y = (P[i] - 1) % N;
    vector<vector<i64>> far(N, vector<i64>(N, 1e9));
    priority_queue<pair<i64, pair<i64, i64>>, vector<pair<i64, pair<i64, i64>>>,
                   greater<pair<i64, pair<i64, i64>>>>
        que;
    far[x][y] = 0;
    que.push({0, {x, y}});
    while (que.size()) {
      pair<i64, pair<i64, i64>> p = que.top();
      que.pop();
      for (i64 j = 0; j < 4; j++) {
        i64 ddx = p.second.first + dx[j], ddy = p.second.second + dy[j];
        if (ddx < 0 || N <= ddx || ddy < 0 || N <= ddy) {
          ans += p.first;
          goto ok;
        }
        if (far[p.second.first][p.second.second] + (use[ddx][ddy]) <
            far[ddx][ddy]) {
          far[ddx][ddy] =
              far[p.second.first][p.second.second] + (use[ddx][ddy]);
          que.push({far[ddx][ddy], {ddx, ddy}});
        }
      }
    }
  ok:;
    use[x][y] = false;
  }
  cout << ans << "\n";
  return 0;
}
