#include <bits/stdc++.h>
using namespace std;
int N;
int a[601][601];
vector<int> v;
map<int, int> m;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void bfs(int x, int y, int now, int p) {
  for (int i = 0; i < (4); i++) {
    int xx = x + dx[i];
    int yy = y + dy[i];
    if (xx < 0 || xx >= N || yy < 0 || yy >= N) continue;
    int nxt = now;
    if (m[y * N + x] > m[p]) nxt++;
    if (a[xx][yy] > nxt) {
      a[xx][yy] = nxt;
      bfs(xx, yy, nxt, p);
    }
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < (N * N); i++) {
    int aa;
    cin >> aa;
    aa--;
    v.push_back(aa);
    m[aa] = i;
  }
  for (int i = 0; i < (N); i++) {
    for (int j = 0; j < (N); j++) {
      a[i][j] = min(i, min(j, min(N - 1 - i, N - 1 - j)));
    }
  }
  long long ans = 0;
  for (int i = 0; i < (v.size()); i++) {
    int now = v[i];
    int x = now % N;
    int y = now / N;
    bfs(x, y, a[x][y], v[i]);
    ans += a[x][y];
  }
  cout << ans << endl;
}
