#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long p[n * n];
  for (long long i = (0); i < (n * n); i++) {
    cin >> p[i];
    p[i]--;
  }
  long long cnt[n][n];
  for (long long i = (0); i < (n); i++)
    for (long long j = (0); j < (n); j++)
      cnt[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
  long long ans = 0;
  bool out[n][n];
  memset(out, 0, sizeof(out));
  for (long long i = (0); i < (n * n); i++) {
    long long x = p[i] / n, y = p[i] % n;
    out[x][y] = true;
    ans += cnt[x][y];
    queue<vector<long long>> q;
    q.push({x, y, cnt[x][y]});
    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      cnt[v[0]][v[1]] = v[2];
      if (!out[v[0]][v[1]]) v[2]++;
      if (v[0] != 0 && cnt[v[0] - 1][v[1]] > v[2])
        q.push({v[0] - 1, v[1], v[2]});
      if (v[0] != n - 1 && cnt[v[0] + 1][v[1]] > v[2])
        q.push({v[0] + 1, v[1], v[2]});
      if (v[1] != 0 && cnt[v[0]][v[1] - 1] > v[2])
        q.push({v[0], v[1] - 1, v[2]});
      if (v[1] != n - 1 && cnt[v[0]][v[1] + 1] > v[2])
        q.push({v[0], v[1] + 1, v[2]});
    }
  }
  cout << (ans) << endl;
  return 0;
}
