#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> p(n * n);
  for (int i = 0; i < n * n; i++) cin >> p[i], p[i]--;
  vector<bool> being(n * n, true);
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    being[p[i]] = false;
    queue<P> que;
    vector<bool> seen(n * n, false);
    que.emplace(p[i], 0);
    bool flag = false;
    int mn = 1e9;
    while (!que.empty()) {
      P v = que.front();
      que.pop();
      if (seen[v.first]) continue;
      seen[v.first] = true;
      int x = v.first % n;
      int y = v.first / n;
      if (x == 0 || x == n - 1 || y == 0 || y == n - 1) {
        mn = min(mn, v.second);
        continue;
      }
      for (int k = 0; k < 4; k++) {
        int nx = x + dx[k];
        int ny = y + dy[k];
        int np = nx + ny * n;
        if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
          continue;
        }
        if (being[np])
          que.emplace(np, v.second + 1);
        else
          que.emplace(np, v.second);
      }
    }
    ans += mn;
  }
  cout << ans << '\n';
  return 0;
}
