#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60;
const long long MOD = 1000000000 + 7;
int main() {
  int n;
  cin >> n;
  int p[n * n];
  vector<int> tate[n + 1], yoko[n + 1];
  for (int i = 0; i < (int)(n); i++) {
    for (int j = 0; j < (int)(n); j++) {
      cin >> p[n * i + j];
      yoko[i + 1].push_back(n * i + j + 1);
      tate[j + 1].push_back(n * i + j + 1);
    }
  }
  unsigned long long hate = 0;
  for (int i = 0; i < (int)(n * n); i++) {
    int lef, rig, up, dow;
    int hor, ver;
    hor = (p[i] - 1) / n + 1;
    ver = p[i] % n;
    if (ver == 0) ver = n;
    auto hor_she = find(yoko[hor].begin(), yoko[hor].end(), p[i]);
    auto ver_she = find(tate[ver].begin(), tate[ver].end(), p[i]);
    lef = hor_she - yoko[hor].begin();
    rig = yoko[hor].end() - hor_she - 1;
    up = ver_she - tate[ver].begin();
    dow = tate[ver].end() - ver_she - 1;
    int min_hate = ((lef) > (rig) ? (rig) : (lef));
    min_hate = ((min_hate) > (up) ? (up) : (min_hate));
    min_hate = ((min_hate) > (dow) ? (dow) : (min_hate));
    hate += min_hate;
    yoko[hor].erase(hor_she);
    tate[ver].erase(ver_she);
  }
  cout << hate << endl;
  return 0;
}
