#include <bits/stdc++.h>
using namespace std;
int n;
int dp[501][501];
bool a[501][501];
int dfs(int i, int j) {
  if (i < 0 or j < 0 or i >= n or j >= n) return 0;
  int &tmp = dp[i][j];
  if (tmp != -1) return tmp;
  tmp = dfs(i - 1, j) + a[i][j];
  tmp = min(tmp, dfs(i, j - 1) + a[i][j]);
  tmp = min(tmp, dfs(i + 1, j) + a[i][j]);
  tmp = min(tmp, dfs(i, j + 1) + a[i][j]);
  return tmp;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n;
  vector<int> v(n * n);
  for (int &i : v) cin >> i;
  memset(a, true, sizeof a);
  int ans = 0;
  for (int &i : v) {
    i--;
    int x = i / n;
    int y = i % n;
    a[x][y] = false;
    memset(dp, -1, sizeof dp);
    ans += dfs(x, y);
  }
  cout << ans << '\n';
}
