#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
int N;
int a[510][510];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
long long dist[500][500];
long long bfs(int sx, int sy) {
  if (sx == 0 || sy == 0 || sx == (N - 1) || sy == (N - 1)) {
    a[sx][sy] = -1;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dist[i][j] = inf;
    }
  }
  long long ans = inf;
  queue<pair<int, int>> que;
  que.push({sx, sy});
  dist[sx][sy] = 0;
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || ny < 0 || nx >= N || ny >= N) {
        ans = min(ans, dist[x][y]);
        break;
      }
      if (dist[nx][ny] != inf) continue;
      if (a[nx][ny] == -1) {
        dist[nx][ny] = dist[x][y];
      } else {
        dist[nx][ny] = dist[x][y] + 1;
      }
      que.push({nx, ny});
    }
  }
  a[sx][sy] = -1;
  return ans;
}
int main() {
  cin >> N;
  int p[N * N];
  for (int i = 0; i < N * N; i++) {
    cin >> p[i];
    p[i]--;
  }
  int tmp = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      a[i][j] = tmp++;
    }
  }
  long long ans = 0;
  for (int i = 0; i < N * N; i++) {
    int x = p[i] / N;
    int y = p[i] % N;
    ans += bfs(x, y);
  }
  cout << ans << endl;
}
