#include <bits/stdc++.h>
using namespace std;
int n;
bool in_bounds(int x, int y) { return (x > -1 && x < n && y > -1 && y < n); }
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  int mi[n][n];
  bool alive[n][n];
  int a[n * n];
  for (int i = 0; i < n * n; i++) cin >> a[i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      mi[i][j] = i;
      mi[i][j] = min(mi[i][j], n - 1 - i);
      mi[i][j] = min(mi[i][j], j);
      mi[i][j] = min(mi[i][j], n - 1 - j);
      alive[i][j] = 1;
    }
  int dx[4] = {0, 0, 1, -1};
  int dy[4] = {1, -1, 0, 0};
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int k = a[i];
    int x = k / n;
    int y = k % n;
    if (!y)
      x--, y = n - 1;
    else
      y--;
    ans += mi[x][y];
    queue<pair<int, int>> bfs;
    bfs.push({x, y});
    alive[x][y] = 0;
    while (!bfs.empty()) {
      int x = bfs.front().first, y = bfs.front().second;
      bfs.pop();
      for (int i = 0; i < 4; i++)
        if (in_bounds(x + dx[i], y + dy[i]) &&
            mi[x + dx[i]][y + dy[i]] > mi[x][y] + alive[x][y])
          mi[x + dx[i]][y + dy[i]] = mi[x][y], bfs.push({x + dx[i], y + dy[i]});
    }
  }
  cout << ans << "\n";
  return 0;
}
