#include <bits/stdc++.h>
using namespace std;
int dist[505][505], n, p[250005];
bool f[505][505];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int cnt;
inline void go(int vv) {
  int x = (int)((vv - 1) / n) + 1, y = (vv - 1) % n + 1;
  f[x][y] = 0;
  memset(dist, 63, sizeof(dist));
  priority_queue<pair<int, pair<int, int> > > pq;
  pq.push({0, {x, y}});
  dist[x][y] = 0;
  for (; pq.size();) {
    pair<int, pair<int, int> > p = pq.top();
    pq.pop();
    x = p.second.first, y = p.second.second;
    int d = -p.first;
    if (dist[x][y] != d) continue;
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i], ty = y + dy[i];
      if (tx < 1 or tx > n or ty < 1 or ty > n) continue;
      if (dist[tx][ty] > dist[x][y] + f[tx][ty]) {
        dist[tx][ty] = dist[x][y] + f[tx][ty];
        pq.push({-dist[tx][ty], {tx, ty}});
      }
    }
  }
  int ans = 1234567;
  for (int i = 1; i <= 500; ++i)
    ans =
        min(ans, min(min(dist[i][1], dist[i][n]), min(dist[1][i], dist[n][i])));
  cnt += ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) f[i][j] = 1;
  for (int i = 1; i <= n * n; ++i) {
    cin >> p[i];
    go(p[i]);
  }
  cout << cnt << '\n';
}
