#include <bits/stdc++.h>
using namespace std;
int n;
int arr[250005];
int grid[505][505];
int dist[505][505];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
queue<pair<long long, long long> > q;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> arr[i];
    arr[i]--;
    dist[i / n][i % n] =
        min(min(i / n, i % n), min(n - 1 - (i / n), n - 1 - (i % n)));
    grid[i / n][i % n] = 1;
  }
  long long answer = 0;
  for (int i = 0; i < n * n; i++) {
    answer += dist[arr[i] / n][arr[i] % n];
    grid[arr[i] / n][arr[i] % n] = 0;
    q.push(make_pair(arr[i] / n, arr[i] % n));
    while (!q.empty()) {
      pair<long long, long long> cur = q.front();
      q.pop();
      int xx = cur.first;
      int yy = cur.second;
      for (int j = 0; j < 4; j++) {
        int newx = xx + dx[j];
        int newy = yy + dy[j];
        if (newx < 0 || newx >= n) continue;
        if (newy < 0 || newy >= n) continue;
        if (dist[newx][newy] > dist[xx][yy] + grid[xx][yy]) {
          dist[newx][newy] = dist[xx][yy] + grid[xx][yy];
          q.push(make_pair(newx, newy));
          break;
        }
      }
    }
  }
  cout << answer << "\n";
}
