#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const long long int inf64 = 1e18 + 5;
const int MAX = 505;
int dp[MAX][MAX];
bool vis[MAX][MAX];
bool done[MAX][MAX];
vector<pair<int, int> > v;
void rec(int x, int y, int n, int val) {
  if (x < 0 || y < 0 || x >= n || y >= n || vis[x][y] || dp[x][y] <= val)
    return;
  vis[x][y] = true;
  dp[x][y] = val;
  v.push_back({x, y});
  rec(x + 1, y, n, dp[x][y] + (!done[x][y]));
  rec(x - 1, y, n, dp[x][y] + (!done[x][y]));
  rec(x, y + 1, n, dp[x][y] + (!done[x][y]));
  rec(x, y - 1, n, dp[x][y] + (!done[x][y]));
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, i, j;
  cin >> n;
  int arr[n * n];
  for (i = 0; i < n * n; i++) cin >> arr[i];
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) dp[i][j] = min({i, j, n - i - 1, n - j - 1});
  }
  long long int ans = 0;
  for (i = 0; i < n * n; i++) {
    --arr[i];
    pair<int, int> p = {arr[i] / n, arr[i] % n};
    ans += dp[p.first][p.second];
    rec(p.first + 1, p.second, n, dp[p.first][p.second]);
    rec(p.first - 1, p.second, n, dp[p.first][p.second]);
    rec(p.first, p.second + 1, n, dp[p.first][p.second]);
    rec(p.first, p.second - 1, n, dp[p.first][p.second]);
    done[p.first][p.second] = 1;
  }
  cout << ans << '\n';
}
