#include <bits/stdc++.h>
using namespace std;
const long long int mod = 998244353;
int a[501][501], n;
bool b[501][501];
void dfs(int i, int j, int y);
void dfs1(int i, int j, int x) {
  if (i < 0 || j < 0 || i == n || j == n) return;
  if (a[i][j] <= x) return;
  a[i][j] = x;
  dfs(i, j, x + b[i][j]);
}
void dfs(int i, int j, int y) {
  dfs1(i + 1, j, y);
  dfs1(i - 1, j, y);
  dfs1(i, j + 1, y);
  dfs1(i, j - 1, y);
}
void solve() {
  cin >> n;
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      a[i][j] = min(min(i, j), min(n - 1 - i, n - 1 - j));
      b[i][j] = 1;
    }
  for (int i = 0; i < n * n; i++) {
    int x;
    cin >> x;
    x--;
    int i1 = x / n;
    int j1 = x % n;
    b[i1][j1] = 0;
    ans += a[i1][j1];
    dfs(i1, j1, a[i1][j1]);
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int test_case = 1; test_case <= t; ++test_case) {
    solve();
  }
}
