#include <bits/stdc++.h>
using namespace std;
const int mod = 1812447359;
const int mod2 = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const int MAX = 200005;
const int ZSUV = 10 * 1000 * 1000;
int d[505][505];
int used[505][505];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
void dfs(int r, int c, int val) {
  d[r][c] = val;
  for (int i = 0; i < 4; i++) {
    int newR = r + dx[i];
    int newC = c + dy[i];
    if (used[newR][newC] && d[newR][newC] > val) dfs(newR, newC, val);
    if (!used[newR][newC] && d[newR][newC] > val) d[newR][newC] = val;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  int ans = 0;
  vector<int> a(n * n);
  for (auto &first : a) cin >> first;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) d[i][j] = min({i - 1, n - i, j - 1, n - j});
  for (auto first : a) {
    first--;
    int r = first / n, c = first % n;
    r++;
    c++;
    dfs(r, c, d[r][c]);
    ans += d[r][c];
    used[r][c] = 1;
  }
  cout << ans;
}
