#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int gcd(int A, int B) {
  if (!B) return A;
  return gcd(B, A % B);
}
vector<vector<bool>> adj(500, vector<bool>(500, 1));
long long int a[250005];
long long int n;
pair<int, int> dir[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int bfs(int s1, int s2) {
  vector<vector<bool>> vis(n, vector<bool>(n, 0));
  vector<vector<long long int>> d(
      n, vector<long long int>(n, (long long int)(1e18)));
  d[s1][s2] = 0;
  deque<pair<int, int>> q;
  q.push_back({s1, s2});
  while (!q.empty()) {
    pair<int, int> p = q.front();
    vis[p.first][p.second] = 1;
    q.pop_front();
    if (p.first == n - 1 || p.second == n - 1 || p.first == 0 || p.second == 0)
      return d[p.first][p.second];
    for (int i = 0; i < 4; i++) {
      int x1 = p.first + dir[i].first;
      int y1 = p.second + dir[i].second;
      if (x1 > n || x1 < 0 || y1 > n || y1 < 0 || vis[x1][y1]) continue;
      if (d[x1][y1] > d[p.first][p.second] + adj[x1][y1])
        d[x1][y1] = d[p.first][p.second] + adj[x1][y1];
      if (p.first == n - 1 || p.second == n - 1 || p.first == 0 ||
          p.second == 0)
        return d[p.first][p.second];
      if (adj[x1][y1])
        q.push_back({x1, y1});
      else
        q.push_front({x1, y1});
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> a[i];
    a[i]--;
  };
  long long int ans = 0;
  for (int i = 0; i < n * n; i++) {
    int y = a[i] % n;
    int x = (a[i] - y) / n;
    adj[x][y] = 0;
    ans += bfs(x, y);
  }
  cout << ans;
}
