#include <bits/stdc++.h>
using namespace std;
int N, arr[505 * 505], dis[505][505], vals[505][505],
    moves[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int t_dis[505][505], upd[505][505], cnt;
inline void bfs(int r, int c) {
  deque<pair<int, int> > q;
  ++cnt;
  q.push_back(make_pair(r, c));
  upd[r][c] = cnt;
  t_dis[r][c] = 0;
  vector<pair<int, int> > v;
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    if (!(x == r && y == c)) v.push_back(make_pair(x, y));
    for (int i = 0; i < 4; i++) {
      int nx = x + moves[i][0], ny = y + moves[i][1];
      if (dis[nx][ny] &&
          (upd[nx][ny] != cnt || t_dis[x][ny] > t_dis[x][y] + vals[nx][ny])) {
        upd[nx][ny] = cnt;
        t_dis[nx][ny] = t_dis[x][y] + vals[nx][ny];
        if (vals[nx][ny] == 0)
          q.push_front(make_pair(nx, ny));
        else
          q.push_back(make_pair(nx, ny));
      }
    }
  }
  for (pair<int, int> check : v)
    dis[check.first][check.second] =
        min(dis[check.first][check.second],
            dis[r][c] + t_dis[check.first][check.second] - 1);
}
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  ;
  cin >> N;
  for (int i = 1; i <= N * N; i++) {
    cin >> arr[i];
    arr[i]--;
  }
  for (int i = 1; i <= N; i++) {
    for (int k = 1; k <= N; k++) {
      dis[i][k] = min({i - 1, k - 1, N - i, N - k});
      vals[i][k] = 1;
    }
  }
  long long res = 0;
  for (int i = 1; i <= N * N; i++) {
    int r = arr[i] / N + 1, c = arr[i] % N + 1;
    vals[r][c] = 0;
    bfs(r, c);
    res += dis[r][c];
  }
  cout << res << '\n';
}
