#include <bits/stdc++.h>
double Pi = 3.141592653589793238;
using namespace std;
using Graph = vector<vector<long long>>;
using Field = vector<string>;
using DGraph = vector<vector<pair<int, int>>>;
const long long INF = 1LL << 60;
bool on[510][510];
int seen[510][510];
int N;
int mn, P[250010];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void dfs(int x, int y) {
  if (seen[x][y] == -1) seen[x][y] = 0;
  if (x >= N - 1 || y >= N - 1 || x <= 0 || y <= 0) {
    mn = min(mn, seen[x][y]);
    return;
  }
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    int be = on[nx][ny];
    if (seen[nx][ny] + be >= mn) continue;
    if (seen[nx][ny] == -1 || seen[nx][ny] > seen[x][y] + be) {
      seen[nx][ny] = seen[x][y] + be;
      dfs(nx, ny);
    }
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N * N; i++) cin >> P[i];
  for (int i = 0; i < 510; i++)
    for (int j = 0; j < 510; j++) on[i][j] = 1;
  int ct = 0;
  for (int i = 0; i < N * N; i++) {
    for (int i = 0; i < 510; i++)
      for (int j = 0; j < 510; j++) seen[i][j] = -1;
    int itr = P[i] - 1;
    mn = min(min(N - 1 - itr / N, itr / N), min(N - 1 - itr % N, itr % N));
    dfs(itr / N, itr % N);
    on[itr / N][itr % N] = 0;
    ct += mn;
  }
  cout << ct << endl;
  return 0;
}
