#include <bits/stdc++.h>
using namespace std;
int f(int i, int n, vector<bool> &p, int c, vector<int> steps) {
  if (find(steps.begin(), steps.end(), i) != steps.end()) return n * n;
  steps.push_back(i);
  if (i - n < 0) return c;
  if (i + n > n * n) return c;
  if (i % n == 0) return c;
  if ((i - 1) % n == 0) return c;
  int ret = n;
  ret = min(ret, f(i + 1, n, p, c + (p.at(i + 1 - 1) ? 0 : 1), steps));
  ret = min(ret, f(i - 1, n, p, c + (p.at(i - 1 - 1) ? 0 : 1), steps));
  ret = min(ret, f(i + n, n, p, c + (p.at(i + n - 1) ? 0 : 1), steps));
  ret = min(ret, f(i - n, n, p, c + (p.at(i - n - 1) ? 0 : 1), steps));
  return ret;
}
int main() {
  int n;
  cin >> n;
  vector<bool> p(n * n);
  for (int i = 0; i < n * n; i++) p.at(i) = false;
  int tp, ret = 0;
  for (int i = 0; i < n * n; i++) {
    cin >> tp;
    vector<int> steps;
    int c = 0;
    ret += f(tp, n, p, c, steps);
    p.at(tp - 1) = true;
  }
  cout << ret << endl;
  return 0;
}
