#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int> > pp(n, vector<int>(n, 0));
  int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int p;
    cin >> p;
    p--;
    int gyou = p / n;
    int retu = p % n;
    int tans = 1000100100;
    int now = gyou;
    now--;
    int tmp = 0;
    while (now >= 0) {
      if (pp[now][retu] <= 0) tmp++;
      now--;
    }
    tans = min(tans, tmp);
    now = gyou;
    now++;
    tmp = 0;
    while (now < n) {
      if (pp[now][retu] <= 0) tmp++;
      now++;
    }
    tans = min(tans, tmp);
    now = retu;
    now++;
    tmp = 0;
    while (now < n) {
      if (pp[gyou][now] <= 0) tmp++;
      now++;
    }
    tans = min(tans, tmp);
    now = retu;
    now--;
    tmp = 0;
    while (now >= 0) {
      if (pp[gyou][now] <= 0) tmp++;
      now--;
    }
    tans = min(tans, tmp);
    ans += tans;
    pp[gyou][retu]++;
  }
  cout << ans << endl;
  return 0;
}
