#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U>
bool chmin(T &a, const U &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int n;
int main() {
  bool flag = false;
  long long ans = 0, sum = 0;
  cin >> n;
  long long nn = n * n;
  vector<vector<int>> od(n, vector<int>(n));
  for (int i = 0, _i = (nn); i < _i; ++i) {
    int pi;
    cin >> pi;
    --pi;
    int ii = pi / n;
    int jj = pi % n;
    od[ii][jj] = i + 1;
  }
  for (int i = 0, _i = (n); i < _i; ++i) {
    for (int j = 0, _i = (n); j < _i; ++j) {
      int l = j;
      int r = n - 1 - j;
      int u = i;
      int d = n - 1 - i;
      int cl = 0, cr = 0, cu = 0, cd = 0;
      for (int k = 0, _i = (l); k < _i; ++k) {
        if (od[i][j - k - 1] > od[i][j]) ++cl;
      }
      for (int k = 0, _i = (r); k < _i; ++k) {
        if (od[i][j + k + 1] > od[i][j]) ++cr;
      }
      for (int k = 0, _i = (u); k < _i; ++k) {
        if (od[i - k - 1][j] > od[i][j]) ++cu;
      }
      for (int k = 0, _i = (d); k < _i; ++k) {
        if (od[i + k + 1][j] > od[i][j]) ++cd;
      }
      int add = min(min(cu, cd), min(cl, cr));
      ans += add;
    }
  }
  cout << ans << endl;
  return 0;
}
