#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f;
const int N = 505;
int val[N][N], dis[N][N];
int n;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
void bfs(int x, int y) {
  queue<pair<int, int>> q;
  q.push({x, y});
  while (!q.empty()) {
    pair<int, int> next = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = next.first + dx[i];
      int ny = next.second + dy[i];
      if (nx == 0 || nx == n + 1 || ny == 0 || ny == n + 1) continue;
      if (dis[nx][ny] >
          dis[next.first][next.second] + val[next.first][next.second]) {
        dis[nx][ny] =
            dis[next.first][next.second] + val[next.first][next.second];
        q.push({nx, ny});
      }
    }
  }
}
int main() {
  long long ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      dis[i][j] = n;
      val[i][j] = 1;
    }
  int t;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> t;
      int x = (t + n - 1) / n;
      int y = (t % n == 0 ? n : t % n);
      val[x][y] = 0;
      bfs(x, y);
      ans += dis[x][y];
    }
  cout << ans << endl;
  return 0;
}
