#include <bits/stdc++.h>
using namespace std;
mt19937_64 mt(time(0));
const int MAXN = 512;
int emp[MAXN][MAXN];
int dp[MAXN][MAXN];
bool fr[MAXN][MAXN];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int solve(int x, int y, int n) {
  memset(dp, -1, sizeof(dp));
  dp[x][y] = 0;
  deque<pair<int, int> > dq;
  dq.emplace_back(x, y);
  while (!dq.empty()) {
    pair<int, int> cur = dq.front();
    dq.pop_front();
    if (fr[cur.first][cur.second]) return dp[cur.first][cur.second];
    for (int k = 0; k < 4; k++) {
      int i = dx[k] + cur.first;
      int j = dy[k] + cur.second;
      if (i < 0 || i >= n || j < 0 || j >= n) {
        if (dp[cur.first][cur.second] == 0) {
          fr[cur.first][cur.second] = true;
        }
        return dp[cur.first][cur.second];
      }
      if (dp[i][j] == -1) {
        dp[i][j] = (1 - emp[i][j]) + dp[cur.first][cur.second];
        if (emp[i][j])
          dq.emplace_front(i, j);
        else
          dq.emplace_back(i, j);
      } else {
        assert(dp[i][j] <= (1 - emp[i][j]) + dp[cur.first][cur.second]);
      }
    }
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  int n, val;
  cin >> n;
  int ret = 0;
  for (int i = 0; i < n * n; i++) {
    cin >> val;
    val--;
    ret += solve(val / n, val % n, n);
    emp[val / n][val % n] = 1;
  }
  cout << ret << '\n';
  return 0;
}
