#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int n, p[250010], ans = 1e9, step[510][510];
bool grid[510][510];
bool isvalid(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= n; }
void bfs(int x, int y) {
  memset(step, 0x3f, sizeof(step));
  queue<pair<int, int> > q;
  q.push(make_pair(x, y));
  step[x][y] = 0;
  while (!q.empty()) {
    int nx = q.front().first, ny = q.front().second;
    q.pop();
    for (int i = 0; i < 4; i++) {
      int tx = nx + dx[i], ty = ny + dy[i];
      if (isvalid(tx, ty)) {
        if (step[tx][ty] > 1e7) {
          step[tx][ty] = step[nx][ny] + grid[tx][ty];
          q.push(make_pair(tx, ty));
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) ans = min(ans, step[1][i]);
  for (int i = 1; i <= n; i++) ans = min(ans, step[i][1]);
  for (int i = 1; i <= n; i++) ans = min(ans, step[i][n]);
  for (int i = 1; i <= n; i++) ans = min(ans, step[n][i]);
}
int main() {
  cin >> n;
  for (int i = 0; i < n * n; i++) cin >> p[i];
  int sum = 0;
  memset(grid, true, sizeof(grid));
  for (int i = 0; i < n * n; i++) {
    ans = 1e9;
    bfs((p[i] - 1) / n + 1, (p[i] - 1) % n + 1);
    sum += ans;
    grid[(p[i] - 1) / n + 1][(p[i] - 1) % n + 1] = false;
  }
  cout << sum << endl;
  return 0;
}
