#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int m,n,k;

int main () {
  long i = 0;
  long count = 0;

  cin >> m;

  long n = m * m;
  vector<long> v(n);

  for (i = 0; i < n; i++){
    cin >> k;
    v[i] = k;
  }

  for (i = 0; i < n; i++){
    if ((v[i] <= n) || (v[i] >= (n * (n - 1))+ 1) || (v[i] % n == 1) || (v[i] % n == n - 1)){
      v[i] = 0;
    }
  }

  for (i = 0; i < n; i++){
    if (v[i] != 0){
      long h = v[i]; 
      long syou = i / n;
      long p = n * syou;
      long q = ((n * (syou + 1)) - 1);
      long cb = count_if(v.begin(), v.end(), [&h, &p] (int x) {return x >= p && x < h;});
      long ca = count_if(v.begin(), v.end(), [&h, &q] (int y) {return y > h && y <= q;});
    }

    if (cb > ca){
      count += ca;
    }

    else{
      count += cb;
    }
    
  cout << cb <<endl;
      
}