#include <bits/stdc++.h>
using namespace std;
const long long N = 507;
long long dist[N][N];
vector<pair<long long, long long> > v = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
bool bad[N][N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    for (long long j = 0; j < n; ++j) {
      dist[i][j] = min(min(i, j), min(n - i - 1, n - j - 1));
      bad[i][j] = 1;
    }
  }
  long long ans = 0;
  for (long long t = 0; t < n * n; ++t) {
    long long who;
    cin >> who;
    --who;
    long long i = who / n;
    long long j = who % n;
    bad[i][j] = 0;
    ans += dist[i][j];
    queue<pair<long long, long long> > q;
    q.push(make_pair(i, j));
    while (q.size()) {
      auto p = q.front();
      q.pop();
      long long i = p.first, j = p.second;
      for (auto e : v) {
        long long x = i + e.first, y = j + e.second;
        if (dist[i][j] + bad[i][j] < dist[x][y]) {
          dist[x][y] = dist[i][j] + bad[i][j];
          q.push(make_pair(x, y));
        }
      }
    }
  }
  cout << ans << '\n';
}
