#include <bits/stdc++.h>
using namespace std;
const int maxn = 200009;
const int MOD = 1e9 + 7;
int N;
int ans = 0;
int black[555][555], vis[555][555];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void bfs(int x, int y) {
  vector<pair<int, int>> que1, que2;
  que1.emplace_back(0, 0);
  memset(vis, -1, sizeof vis);
  vis[0][0] = 0;
  while (1) {
    que2.clear();
    for (int i = 0; i < que1.size(); ++i) {
      for (int j = 0; j < 4; ++j) {
        int nx = que1[i].first + dx[j], ny = que1[i].second + dy[j];
        if (nx >= 0 && nx <= N + 1 && ny >= 0 && ny <= N + 1 &&
            vis[nx][ny] == -1) {
          if (black[nx][ny]) {
            vis[nx][ny] = vis[que1[i].first][que1[i].second] + 1;
            que2.emplace_back(nx, ny);
            if (nx == x && ny == y) {
              ans += vis[nx][ny];
              return;
            }
          } else {
            vis[nx][ny] = vis[que1[i].first][que1[i].second];
            que1.emplace_back(nx, ny);
            if (nx == x && ny == y) {
              ans += vis[nx][ny];
              return;
            }
          }
        }
      }
    }
    swap(que1, que2);
  }
}
int main() {
  ;
  ios_base::sync_with_stdio(false), cout.tie(0), cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; ++i)
    for (int j = 1; j <= N; ++j) black[i][j] = 1;
  for (int i = 0; i < N * N; ++i) {
    int t;
    cin >> t;
    t--;
    black[t / N + 1][t % N + 1] = 0;
    bfs(t / N + 1, t % N + 1);
  }
  cout << ans << '\n';
  return 0;
}
