#include <bits/stdc++.h>
using namespace std;
const int maxn = 500;
int vis[maxn + 5][maxn + 5];
int p[maxn * maxn + 5];
int n;
int cal(int x, int y) {
  if (y == 0) y = n;
  int res1 = 0, res2 = 0, res3 = 0, res4 = 0;
  for (int i = x - 1; i >= 1; --i)
    if (!vis[i][y]) ++res1;
  for (int i = x + 1; i <= n; ++i)
    if (!vis[i][y]) ++res2;
  for (int j = y - 1; j >= 1; --j)
    if (!vis[x][j]) ++res3;
  for (int j = y + 1; j <= n; ++j)
    if (!vis[x][j]) ++res4;
  vis[x][y] = 1;
  return min(res1, min(res2, min(res3, res4)));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n * n; ++i) cin >> p[i];
  int ans = 0;
  for (int i = 1; i <= n * n; ++i) {
    int x = p[i];
    ans += cal((x - 1) / n + 1, x % n);
  }
  cout << ans << endl;
  return 0;
}
