#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int INF = 100100100;
int main() {
  int N;
  cin >> N;
  vector<int> P(N * N);
  for (int ii = 0; ii < N * N; ++ii) {
    cin >> P[ii];
    P[ii]--;
  }
  long long ans = 0;
  vector<vector<bool>> kara(N, vector<bool>(N, true));
  for (int ii = N * N; ii >= 0; --ii) {
    int x = P[ii] % N;
    int y = P[ii] / N;
    queue<pair<int, int>> q;
    q.push(make_pair(x, y));
    kara[x][y] = false;
    int minValue = INF;
    vector<vector<int>> dist(N, vector<int>(N, INF));
    dist[x][y] = 0;
    int nx[4] = {1, -1, 0, 0};
    int ny[4] = {0, 0, 1, -1};
    while (!q.empty()) {
      int tx = q.front().first;
      int ty = q.front().second;
      q.pop();
      for (int ii = 0; ii < 4; ++ii) {
        int x = tx + nx[ii];
        int y = ty + ny[ii];
        if (x < 0 || x >= N || y < 0 || y >= N) {
          minValue = min(minValue, dist[tx][ty]);
          continue;
        }
        int tmp = (kara[x][y]) ? (dist[tx][ty]) : dist[tx][ty] + 1;
        if ((dist[x][y] == INF) || (tmp < dist[x][y])) {
          dist[x][y] = tmp;
          q.push(make_pair(x, y));
        }
      }
    }
    ans += minValue;
  }
  cout << ans << endl;
  return 0;
}
