#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 510;
int n, dis[maxn][maxn], od[maxn][maxn];
int p[maxn];
struct info {
  int cost, x, y;
  bool operator<(const info &b) const { return cost > b.cost; }
};
bool vis[maxn][maxn];
int get_ans(int x, int y) {
  memset(dis, 0x3f, sizeof(dis));
  memset(vis, 0, sizeof(vis));
  dis[x][y] = 0;
  priority_queue<info> togo;
  togo.push({0, x, y});
  int res = n * n, O = od[x][y], ox = x, oy = y;
  while (!togo.empty()) {
    auto [cost, x, y] = togo.top();
    togo.pop();
    if (x == 0 || y == 0 || x == n + 1 || y == n + 1) {
      res = min(res, cost);
      continue;
    }
    if (vis[x][y]) continue;
    vis[x][y] = true;
    for (int i : {-1, 0, 1})
      for (int j : {-1, 0, 1})
        if (abs(i) + abs(j) == 1) {
          int nx = x + i, ny = j + y, val = cost + (od[nx][ny] > O);
          if (val < dis[nx][ny]) {
            dis[nx][ny] = val;
            togo.push({val, nx, ny});
          }
        }
  }
  return res;
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n * n; ++i) {
    cin >> p[i];
    --p[i];
    int r = p[i] / n, c = p[i] % n;
    od[r + 1][c + 1] = i;
  }
  int res = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) res += get_ans(i, j);
  cout << res << '\n';
}
