#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vel = vector<ll>;
using ves = vector<string>;
using veb = vector<bool>;
using qul = queue<ll>;
int main() {
  ll N, up, down, left, right, num, cu = 0, cd = 0, cl = 0, cr = 0, count = 0;
  cin >> N;
  vel check(N * N, 0);
  for (ll i = 0; i < (ll)(N * N); i++) {
    cin >> num;
    num--;
    check[num] = 1;
    up = num / N;
    down = N - up - 1;
    left = num % N;
    right = N - left - 1;
    for (ll j = 0; j < (ll)(up); j++) {
      if (check[num - ((j + 1) * N)] == 0) cu++;
    }
    for (ll j = 0; j < (ll)(down); j++) {
      if (check[num + ((j + 1) * N)] == 0) cd++;
    }
    for (ll j = 0; j < (ll)(left); j++) {
      if (check[num - (j + 1)] == 0) cl++;
    }
    for (ll j = 0; j < (ll)(right); j++) {
      if (check[num + (j + 1)] == 0) cr++;
    }
    count += min(min(cu, cd), min(cl, cr));
    cu = 0;
    cd = 0;
    cl = 0;
    cr = 0;
  }
  cout << count << endl;
}
