#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
bool exist[500][500];
bool zero[500][500];
bool visited[500][500];
int n;
int reach(int x, int y) {
  if (visited[x][y]) return 9999;
  if (x < 0 || x >= n || y < 0 || y >= n) {
    return 0;
  }
  if (zero[x][y]) return 0;
  visited[x][y] = true;
  int mini = reach(x + 1, y);
  if (mini > 0) mini = min(mini, reach(x - 1, y));
  if (mini > 0) mini = min(mini, reach(x, y + 1));
  if (mini > 0) mini = min(mini, reach(x, y - 1));
  visited[x][y] = false;
  if (exist[x][y]) mini++;
  if (mini == 0) zero[x][y] = true;
  return mini;
}
int main() {
  cin >> n;
  for (int i = 0; i < (n); ++i) {
    for (int j = 0; j < (n); ++j) {
      exist[i][j] = true;
      zero[i][j] = false;
      visited[i][j] = false;
    }
  }
  int ans = 0;
  for (int i = 0; i < (n * n); ++i) {
    int p;
    cin >> p;
    int x = (p - 1) / n, y = (p - 1) % n;
    exist[x][y] = false;
    int t = reach(x, y);
    ans += t;
  }
  cout << ans << endl;
  return 0;
}
