#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1 << 30;
const long long INFL = 1LL << 60;
const long long mod = 998244353;
vector<int> v;
vector<int> dx = {1, -1, 0, 0, 1, 1, -1, -1};
vector<int> dy = {0, 0, 1, -1, 1, -1, 1, -1};
int ans = 0;
vector<bool> used;
int n;
void bfs(int l) {
  vector<bool> us(n * n, false);
  min_priority_queue<pair<int, int>> q;
  us[l] = true;
  q.push({0, l});
  while (!q.empty()) {
    pair<int, int> a = q.top();
    q.pop();
    int ia = a.second / n;
    int ja = a.second % n;
    for (int k = 0; k < int(4); k++) {
      int ny = ia + dy[k], nx = ja + dx[k];
      if (ny < 0 || n <= ny || nx < 0 || n <= nx) {
        ans += a.first;
        return;
      }
      if (us[ny * n + nx]) continue;
      if (!used[ny * n + nx])
        q.push({a.first + 1, ny * n + nx});
      else
        q.push({a.first, ny * n + nx});
      us[ny * n + nx] = true;
    }
  }
}
int main() {
  cin >> n;
  v.resize(n * n);
  used.resize(n * n);
  for (int i = 0; i < int(n * n); i++) used[i] = false;
  for (int i = 0; i < int(n * n); i++) {
    cin >> v[i];
    v[i]--;
  }
  for (int i = 0; i < int(n * n); i++) {
    if (v[i] % n == 0 || v[i] % n == n - 1 || v[i] / n == 0 ||
        v[i] / n == n - 1) {
      used[v[i]] = true;
      continue;
    }
    bfs(v[i]);
    used[v[i]] = true;
  }
  cout << ans << endl;
}
