#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<vector<int>> vec(n, vector<int>(n, 1));
  vector<int> p(n * n);
  for (long long i = 0; i < (long long)(n * n); i++) cin >> p[i];
  int cnt = 0;
  for (long long i = 0; i < (long long)(n * n); i++) {
    p[i]--;
    int sx = p[i] % n;
    int sy = (p[i] - sx) / n;
    vec[sy][sx] = 0;
    if (sy == 0 || sy == n - 1 || sx == 0 || sx == n - 1) continue;
    queue<pair<long long, long long>> que;
    que.push(pair<long long, long long>(sy, sx));
    int dp[n][n];
    memset(dp, -1, sizeof(dp));
    dp[sy][sx] = 0;
    while (!que.empty()) {
      auto t = que.front();
      que.pop();
      int y = t.first, x = t.second;
      for (long long i = 0; i < (long long)(4); i++) {
        int ny = y + dy[i], nx = x + dx[i];
        if (ny < 0 || ny >= n || nx < 0 || nx >= n || dp[ny][nx] != -1)
          continue;
        dp[ny][nx] = dp[y][x] + vec[ny][nx];
        que.push(pair<long long, long long>(ny, nx));
      }
    }
    int res = 1e9;
    for (long long i = 0; i < (long long)(n); i++) res = min(res, dp[0][i]);
    for (int i = 1; i < n - 1; i++) {
      res = min(res, dp[i][0]);
      res = min(res, dp[i][n - 1]);
    }
    for (long long i = 0; i < (long long)(n); i++) res = min(res, dp[n - 1][i]);
    cnt += res;
  }
  cout << cnt << endl;
  return 0;
}
