#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, P, A;
  int gyou, retu;
  int vec[505][505];
  int vecA[505][505];
  cin >> N;
  long long pen = 0;
  int pena, penb, penc, pend, pent;
  pena = 0;
  penb = 0;
  penc = 0;
  pend = 0;
  pent = 0;
  P = 0;
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++) {
      P++;
      P--;
      gyou = P / N;
      gyou++;
      retu = P % N;
      retu++;
      P++;
      vec[gyou][retu] = P;
      vecA[i + 1][k + 1] = 1;
    }
  }
  for (int i = 0; i < N; i++) {
    for (int k = 0; k < N; k++) {
      cin >> P;
      P--;
      gyou = P / N;
      gyou++;
      retu = P % N;
      retu++;
      P++;
      for (int i1 = 0; i1 < gyou; i1++) {
        if (vecA[i1][retu] == 1) pena++;
      }
      for (int i2 = gyou + 1; i2 < N + 1; i2++) {
        if (vecA[i2][retu] == 1) penb++;
      }
      for (int i3 = 0; i3 < retu; i3++) {
        if (vecA[gyou][i3] == 1) penc++;
      }
      for (int i4 = retu + 1; i4 < N + 1; i4++) {
        if (vecA[gyou][i4] == 1) pend++;
      }
      pent = min({pena, penb, penc, pend});
      pen += pent;
      vecA[gyou][retu] = 0;
      pena = 0;
      penb = 0;
      penc = 0;
      pend = 0;
    }
  }
  cout << pen << endl;
}
