#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e6;
const long long int mod = 1e9 + 7;
const long long int INF = 1e14;
long long int powm(long long int a, long long int b) {
  a = a % mod;
  long long int res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    long long int p[n * n];
    long long int dp[n][n];
    bool is[n][n];
    for (int i = 0; i < n * n; i++) {
      cin >> p[i];
    }
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {
        dp[i][j] = min(i, n - i - 1);
        dp[i][j] = min(dp[i][j], j);
        dp[i][j] = min(dp[i][j], n - j - 1);
        is[i][j] = 1;
      }
    }
    long long int ans = 0;
    queue<pair<int, int> > q;
    for (int i = 0; i < n * n; i++) {
      p[i]--;
      long long int first = p[i] / n, second = p[i] % n;
      ans += dp[first][second];
      q.push(make_pair(first, second));
      is[first][second] = 0;
      while (!q.empty()) {
        pair<int, int> v = q.front();
        q.pop();
        if (v.first > 0) {
          if (dp[v.first][v.second] < dp[v.first - 1][v.second]) {
            dp[v.first - 1][v.second] =
                dp[v.first][v.second] + is[v.first][v.second];
            q.push(make_pair(v.first - 1, v.second));
          }
        }
        if (v.first < n - 1) {
          if (dp[v.first][v.second] < dp[v.first + 1][v.second]) {
            dp[v.first + 1][v.second] =
                dp[v.first][v.second] + is[v.first][v.second];
            q.push(make_pair(v.first + 1, v.second));
          }
        }
        if (v.second < n - 1) {
          if (dp[v.first][v.second] < dp[v.first][v.second + 1]) {
            dp[v.first][v.second + 1] =
                dp[v.first][v.second] + is[v.first][v.second];
            q.push(make_pair(v.first, v.second + 1));
          }
        }
        if (v.second > 0) {
          if (dp[v.first][v.second] < dp[v.first][v.second - 1]) {
            dp[v.first][v.second - 1] =
                dp[v.first][v.second] + is[v.first][v.second];
            q.push(make_pair(v.first, v.second - 1));
          }
        }
      }
    }
    cout << ans << '\n';
  }
}
