#include <bits/stdc++.h>
using namespace std;
long long INF = LLONG_MAX;
int const mxn = 502;
bool a[mxn][mxn], vis[mxn][mxn];
int p[2502], n, ans = 0;
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
bool check(int x, int y) {
  if (1 <= x && x <= n && 1 <= y && y <= n) return 0;
  return 1;
}
void bfs(int x, int y) {
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.emplace(0, pair<int, int>(x, y));
  memset(vis, 0, sizeof(vis));
  while (!pq.empty()) {
    int dist = pq.top().first;
    int xx = pq.top().second.first;
    int yy = pq.top().second.second;
    pq.pop();
    if (vis[xx][yy]) continue;
    vis[xx][yy] = 1;
    if (check(xx, yy)) {
      ans += dist;
      return;
    }
    for (int i = 0; i < 4; i++) {
      int X = xx + dx[i];
      int Y = yy + dy[i];
      pq.emplace(dist + int(a[X][Y] == 1), pair<int, int>(X, Y));
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = 1;
  for (int i = 1; i <= n * n; i++) {
    cin >> p[i];
    p[i]--;
    int x = p[i] / n + 1;
    int y = p[i] % n + 1;
    bfs(x, y);
    a[x][y] = 0;
  }
  cout << ans << endl;
}
