#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, N = 505;
int n;
int liv[N][N], dp[N][N], vis[N][N];
int a[N * N];
int b[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
queue<pair<int, int> > q;
int dfs(int x, int y) {
  int i;
  if (x == 1 || x == n || y == 1 || y == n) return dp[x][y];
  for (i = 0; i < 4; i++) {
    int nx = x + b[i][0], ny = y + b[i][1];
    if ((dp[nx][ny] == -1) && (!liv[nx][ny])) {
      dp[nx][ny] = dp[x][y];
      int tmp = dfs(nx, ny);
      if (tmp >= 0) return tmp;
    } else if (dp[nx][ny] == -1) {
      dp[nx][ny] = dp[x][y] + 1;
      q.push(make_pair(nx, ny));
    }
  }
  return -1;
}
int calc(int x, int y) {
  int i, j;
  memset(vis, 0, sizeof(vis));
  while (!q.empty()) {
    q.pop();
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) dp[i][j] = -1;
  dp[x][y] = 0;
  q.push(make_pair(x, y));
  while (!q.empty()) {
    pair<int, int> s = q.front();
    q.pop();
    int x1 = s.first, y1 = s.second;
    int tmp = dfs(x1, y1);
    if (tmp >= 0) return tmp;
  }
}
int main() {
  int i, j;
  cin >> n;
  for (i = 1; i <= n * n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++) liv[i][j] = 1;
  int ans = 0;
  for (i = 1; i <= n * n; i++) {
    int x = (a[i] - 1) / n + 1, y = (a[i] - 1) % n + 1;
    int tmp = calc(x, y);
    ans += tmp;
    liv[x][y] = 0;
  }
  cout << ans << endl;
}
