#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 10 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long grid[MAXN][MAXN];
  bool left[MAXN][MAXN];
  long long n;
  cin >> n;
  for (long long i = (1); i <= (n); i++) {
    for (long long j = (1); j <= (n); j++) {
      long long vert = min(i - 1, n - i);
      long long horz = min(j - 1, n - j);
      long long minimum = min(vert, horz);
      grid[i][j] = minimum;
      left[i][j] = 0;
    }
  }
  deque<pair<long long, long long> > bfs;
  long long ans = 0;
  long long c1[4] = {-1, 0, 0, 1};
  long long c2[4] = {0, -1, 1, 0};
  for (long long i = (1); i <= (n * n); i++) {
    long long x;
    cin >> x;
    x--;
    long long row = x / n + 1;
    long long col = x % n + 1;
    ans += grid[row][col];
    grid[row][col]--;
    left[row][col] = 1;
    bfs.emplace_back(row, col);
    while (!bfs.empty()) {
      long long r = bfs.front().first;
      long long c = bfs.front().second;
      bfs.pop_front();
      for (long long j = (0); j <= (3); j++) {
        long long newr = r + c1[j];
        long long newc = c + c2[j];
        if (newr == 0 || newc == 0 || newr > n || newc > n) continue;
        if (grid[r][c] + 1 < grid[newr][newc] && !left[newr][newc]) {
          grid[newr][newc] = grid[r][c] + 1;
          bfs.emplace_back(newr, newc);
        } else if (left[newr][newc] && grid[r][c] < grid[newr][newc]) {
          grid[newr][newc] = grid[r][c];
          bfs.emplace_front(newr, newc);
        }
      }
    }
  }
  cout << ans;
}
