#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MOD2 = 998244353;
const long long MOD3 = 1812447359;
const long long INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() { printf("yes\n"); }
void no() { printf("no\n"); }
void Yes() { printf("Yes\n"); }
void No() { printf("No\n"); }
void YES() { printf("YES\n"); }
void NO() { printf("NO\n"); }
int N, P[int(25e4 + 5)], Dist[505][505];
int di[4] = {0, 1, 0, -1}, dj[4] = {1, 0, -1, 0};
bool visited[505][505], vacant[505][505];
int Calc_Dist() {
  queue<pair<int, int> > que;
  for (int i = 0; i < N; i++) {
    que.push({0, i});
    visited[0][i] = true;
    que.push({i, 0});
    visited[i][0] = true;
    que.push({N - 1, i});
    visited[N - 1][i] = true;
    que.push({i, N - 1});
    visited[i][N - 1] = true;
  }
  while (!que.empty()) {
    pair<int, int> Q = que.front();
    que.pop();
    for (int k = 0; k < 4; k++) {
      int I = Q.first + di[k], J = Q.second + dj[k];
      if (0 <= I && I < N && 0 <= J && J < N) {
        if (!visited[I][J]) {
          visited[I][J] = true;
          Dist[I][J] = Dist[Q.first][Q.second] + 1;
          que.push({I, J});
        }
      }
    }
  }
  return 0;
}
int main() {
  cin >> N;
  Calc_Dist();
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
    int I = (P[i] - 1) / N, J = (P[i] - 1) % N;
    ans += Dist[I][J];
    queue<pair<int, int> > que;
    que.push({I, J});
    while (!que.empty()) {
      pair<int, int> Q = que.front();
      que.pop();
      for (int k = 0; k < 4; k++) {
        int II = Q.first + di[k], JJ = Q.second + dj[k];
        if (0 <= II && II < N && 0 <= JJ && JJ < N) {
          if (vacant[II][JJ] || vacant[Q.first][Q.second]) {
            if (Dist[II][JJ] > Dist[Q.first][Q.second]) {
              Dist[II][JJ] = Dist[Q.first][Q.second];
              que.push({II, JJ});
            }
          } else {
            if (Dist[II][JJ] > Dist[Q.first][Q.second] + 1) {
              Dist[II][JJ] = Dist[Q.first][Q.second] + 1;
              que.push({II, JJ});
            }
          }
        }
      }
    }
    vacant[I][J] = true;
  }
  cout << ans << endl;
  return 0;
}
