#include <bits/stdc++.h>
using namespace std;
int N;
vector<pair<int, int>> P;
vector<vector<int>> A;
int count(int sx, int sy) {
  const int inf = 1e9;
  vector<vector<int>> D(N, vector<int>(N, inf));
  priority_queue<tuple<int, int, int>> Q;
  Q.push(make_tuple(0, sx, sy));
  static int dx[] = {1, 0, -1, 0};
  static int dy[] = {0, 1, 0, -1};
  while (true) {
    int d, x, y;
    tie(d, x, y) = Q.top();
    Q.pop();
    d *= -1;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= N) return d;
      if (ny < 0 || ny >= N) return d;
      int nd = d + A[nx][ny];
      if (D[nx][ny] <= nd) continue;
      D[nx][ny] = nd;
      Q.push(make_tuple(-1 * nd, nx, ny));
    }
  }
  throw;
}
int solve() {
  int ans = 0;
  for (auto p : P) {
    int x, y;
    tie(x, y) = p;
    int d = count(x, y);
    ans += d;
    A[x][y] = 0;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  P.assign(N * N, {});
  A.assign(N, vector<int>(N, 1));
  for (int i = 0; i < N * N; ++i) {
    int j;
    cin >> j;
    --j;
    P[i] = {j % N, j / N};
  }
  cout << solve() << endl;
  return 0;
}
