#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, inf = 1e9 + 7;
const long long infll = 1e18 + 7;
bool google_contest = false;
long long myadd(long long a, long long b) {
  long long ans = a + b;
  ans -= ans >= mod ? mod : 0;
  return ans;
}
long long mysub(long long a, long long b) { return (a - b + mod) % mod; }
long long mymul(long long a, long long b) { return (a * b) % mod; }
long long mypow(long long a, long long b) {
  long long ans = 1;
  while (b > 0)
    if (b & 1)
      ans = mymul(ans, a), b--;
    else
      a = mymul(a, a), b >>= 1;
  return ans;
}
long long nCr(long long n, long long r) {
  if (r == 0) return 1;
  if (r == 1) return n;
  if (n == 0) return 0;
  return mymul(n, mymul(nCr(n - 1, r - 1), mypow(r, mod - 2)));
}
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const int maxi = 505;
int dist[maxi][maxi];
vector<pair<int, int>> nei[maxi][maxi];
bool removed[maxi][maxi];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t, tt;
  t = 1;
  if (google_contest) tt = 1;
  while (t--) {
    int n;
    cin >> n;
    for (int row = 0; row < n; ++row) {
      for (int col = 0; col < n; ++col) {
        for (int dir = 0; dir < 4; ++dir) {
          int r2 = row + dx[dir];
          int c2 = col + dy[dir];
          if (0 <= r2 && 0 <= c2 && r2 < n && c2 < n) {
            nei[row][col].emplace_back(r2, c2);
          }
        }
      }
    }
    for (int r = 0; r < n; r++) {
      for (int c = 0; c < n; c++) {
        dist[r][c] = 1 + min({r, c, n - r - 1, n - c - 1});
      }
    }
    long long ans = 0;
    for (int i = 0; i < n * n; i++) {
      int qq;
      cin >> qq;
      qq--;
      int row = qq / n;
      int col = qq % n;
      ans += dist[row][col] - 1;
      cerr << dist[row][col] << '\n';
      dist[row][col]--;
      removed[row][col] = true;
      vector<pair<int, int>> q;
      q.emplace_back(row, col);
      for (int i = 0; i < (int)q.size(); ++i) {
        pair<int, int> a = q[i];
        for (const pair<int, int>& b : nei[a.first][a.second]) {
          int maybe = dist[a.first][a.second];
          if (!removed[b.first][b.second]) {
            maybe++;
          }
          if (dist[b.first][b.second] > maybe) {
            dist[b.first][b.second] = maybe;
            q.push_back(b);
          }
        }
      }
    }
    cout << ans << '\n';
    if (google_contest) {
      cout << "Case " << tt++ << ": ";
    }
  }
  return 0;
}
