#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2,popcnt,fma")
#pragma GCC optimize("fast-math")
using namespace std;
const long long mod = 1e9 + 7, mod2 = 998244353;
const long double eps = 1e-6;
const long long inf = 1e18;
const int base = 1e9;
const long double PI = 3.1415926;
void deb(long double first){};
int n;
vector<int> ord;
vector<vector<int>> v;
vector<int> di = {-1, 0, 1, 0};
vector<int> dj = {0, -1, 0, 1};
unsigned int bitcount(unsigned long long val) {
  return __builtin_popcount(val) + __builtin_popcount(val >> 32);
}
void local() { freopen("test.in", "r", stdin); }
void remote() {}
void read() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  ord.resize(n * n);
  for (int i = 0; i < ord.size(); i++) cin >> ord[i], ord[i]--;
}
int main() {
  remote();
  read();
  v.resize(n, vector<int>(n, 1));
  long long ans = 0;
  for (const int &id : ord) {
    int i = id / n, j = id % n;
    v[i][j] = 0;
    int mn = INT32_MAX, s = 0;
    for (int first = i; first < n; first++) s += v[first][j];
    mn = min(mn, s), s = 0;
    for (int first = 0; first < i; first++) s += v[first][j];
    mn = min(mn, s), s = 0;
    for (int second = j; second < n; second++) s += v[i][second];
    mn = min(mn, s), s = 0;
    for (int second = 0; second < j; second++) s += v[i][second];
    mn = min(mn, s), s = 0;
    ans += mn;
  }
  cout << ans << '\n';
  ;
}
