#include <bits/stdc++.h>
using namespace std;
int mas[500][500];
int dist[500][500];
int vis[500][500];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  deque<pair<int, int> > q;
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    int in;
    cin >> in;
    in--;
    q.push_front({in % n, in / n});
    mas[in % n][in / n] = 1;
    vis[in % n][in / n] = i + 1;
    dist[in % n][in / n] = 0;
    while (!q.empty()) {
      int x = q.front().first, y = q.front().second;
      q.pop_front();
      if (x == 0 || x == n - 1 || y == 0 || y == n - 1) {
        ans += dist[x][y];
        break;
      }
      if (vis[x - 1][y] != i + 1) {
        vis[x - 1][y] = i + 1;
        if (mas[x - 1][y] == 1) {
          dist[x - 1][y] = dist[x][y];
          q.push_front({x - 1, y});
        } else {
          dist[x - 1][y] = dist[x][y] + 1;
          q.push_back({x - 1, y});
        }
      }
      if (vis[x][y - 1] != i + 1) {
        vis[x][y - 1] = i + 1;
        if (mas[x][y - 1] == 1) {
          dist[x][y - 1] = dist[x][y];
          q.push_front({x, y - 1});
        } else {
          dist[x][y - 1] = dist[x][y] + 1;
          q.push_back({x, y - 1});
        }
      }
      if (vis[x + 1][y] != i + 1) {
        vis[x + 1][y] = i + 1;
        if (mas[x + 1][y] == 1) {
          dist[x + 1][y] = dist[x][y];
          q.push_front({x + 1, y});
        } else {
          dist[x + 1][y] = dist[x][y] + 1;
          q.push_back({x + 1, y});
        }
      }
      if (vis[x][y + 1] != i + 1) {
        vis[x][y + 1] = i + 1;
        if (mas[x][y + 1] == 1) {
          dist[x][y + 1] = dist[x][y];
          q.push_front({x, y + 1});
        } else {
          dist[x][y + 1] = dist[x][y] + 1;
          q.push_back({x, y + 1});
        }
      }
    }
    q.clear();
  }
  cout << ans;
}
