#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> seat;
long long n;
long long bfs(long long src) {
  vector<vector<long long>> vis(n + 2, vector<long long>(n + 2));
  vis[(src - 1) / n + 1][(src - 1) % n + 1] = 1;
  queue<tuple<long long, long long, long long>> q;
  q.push({(src - 1) / n + 1, (src - 1) % n + 1, 0});
  auto valid = [&](long long x, long long y) {
    return 0 <= x && x <= n + 1 && 0 <= y && y <= n + 1;
  };
  vector<tuple<long long, long long>> del = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
  long long dx, dy, mx, my;
  long long minn = INT_MAX;
  while (!q.empty()) {
    long long x, y, d;
    tie(x, y, d) = q.front();
    q.pop();
    if (x == 0 || x == n + 1 || y == 0 || y == n + 1) {
      minn = min(minn, d);
      continue;
    }
    for (auto i : del) {
      tie(dx, dy) = i;
      mx = x + dx;
      my = y + dy;
      if (valid(mx, my) && !vis[mx][my]) {
        q.push({mx, my, d + seat[mx][my]});
        vis[mx][my] = 1;
      }
    }
  }
  return minn;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  seat.resize(n + 2, vector<long long>(n + 2));
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= n; ++j) {
      seat[i][j] = 1;
    }
  };
  long long sum = 0;
  for (long long i = 0; i < n * n; ++i) {
    long long a;
    cin >> a;
    sum += bfs(a);
    seat[(a - 1) / n + 1][(a - 1) % n + 1] = 0;
  }
  cout << sum << endl;
}
