#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using vstr = vector<string>;
const ll MOD = 1e9 + 7;
const ll INF = 1ll << 60;
int N;
int main() {
  cin >> N;
  vint p, q;
  for (int i = 0; i < N * N; i++) {
    int pin;
    cin >> pin;
    p.emplace_back(pin - 1);
    q.emplace_back(1);
  }
  int sum = 0;
  for (int i = 0; i < N * N; i++) {
    if (p.at(i) < N || p.at(i) % N == 0 || p.at(i) % N == N - 1 ||
        p.at(i) > N * N - 1 - N) {
    } else {
      int up = 0;
      int down = 0;
      int right = 0;
      int left = 0;
      for (int j = p.at(i) - N; j > 0; j -= N) {
        up += q.at(j);
      }
      for (int j = p.at(i) - 1; !(j % N == N - 1); j--) {
        left += q.at(j);
      }
      for (int j = p.at(i) + 1; !(j % N == 0); j++) {
        right += q.at(j);
      }
      for (int j = p.at(i) + N; j < N * N; j += N) {
        down += q.at(j);
      }
      sum += min(min(up, down), min(right, left));
    }
    q.at(p.at(i)) = 0;
  }
  cout << sum << endl;
  return 0;
}
