#include <bits/stdc++.h>
using namespace std;
int n, h, w, r[500][500] = {0};
char C[500][500];
deque<int> x, y;
int vx[4] = {0, 1, 0, -1}, vy[4] = {1, 0, -1, 0};
int bfs(int sx, int sy) {
  int ans;
  x.clear(), y.clear();
  for (long long i = 0; i < n + 2; i++)
    for (long long j = 0; j < n + 2; j++) r[i][j] = 0;
  r[sx][sy] = 1;
  x.push_front(sx), y.push_front(sy);
  while (x.size()) {
    int a = x.front(), b = y.front();
    x.pop_front(), y.pop_front();
    for (long long i = 0; i < 4; i++) {
      int nx = a + vx[i], ny = b + vy[i];
      if (nx >= 0 && nx < n + 2 && ny >= 0 && ny < n + 2 && r[nx][ny] == 0) {
        if (C[nx][ny] == '#')
          r[nx][ny] = r[a][b] + 1, x.push_back(nx), y.push_back(ny);
        else
          r[nx][ny] = r[a][b], x.push_front(nx), y.push_front(ny);
        if (nx == 0 || nx == n + 1 || ny == 0 || ny == n + 1) return r[nx][ny];
      }
    }
  }
}
int main() {
  int pair<long long, long long>[250001];
  long long ans = 0;
  cin >> n;
  h = n, w = n;
  for (long long i = 0; i < n + 2; i++)
    for (long long j = 0; j < n + 2; j++) C[i][j] = '#';
  for (long long i = 0; i < n + 2; i++)
    C[i][0] = '.', C[0][i] = '.', C[n + 1][i] = '.', C[i][n + 1] = '.';
  for (long long i = 0; i < n * n; i++) cin >> pair<long long, long long>[i];
  for (long long i = 0; i < n * n; i++) {
    ans += bfs((pair<long long, long long>[i] - 1) / n + 1,
               (pair<long long, long long>[i] + n - 1) % n + 1) -
           1;
    C[(pair<long long, long long>[i] - 1) / n + 1]
     [(pair<long long, long long>[i] - 1 + n) % n + 1] = '.';
  }
  cout << ans;
}
