#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
long long int dist[600][600];
long long int has[600][600];
int main() {
  long long int N;
  cin >> N;
  for (long long int i = 1; i < N + 1; i++) {
    for (long long int j = 1; j < N + 1; j++) {
      has[i][j] = 1;
      dist[i][j] = min({i, j, N + 1LL - i, N + 1LL - j}) - 1;
      cout << dist[i][j] << " ";
    }
    cout << endl;
    ;
  }
  long long int _ = N * N;
  long long int ans = 0;
  while (_--) {
    long long int a;
    cin >> a;
    long long int nowI = (a - 1) / N + 1;
    long long int nowJ = (a - 1) % N + 1;
    ans += dist[nowI][nowJ];
    long long int di[4] = {-1, 0, 1, 0};
    long long int dj[4] = {0, 1, 0, -1};
    queue<pair<long long int, long long int>> q;
    queue<long long int> qd;
    has[nowI][nowJ] = 0;
    q.push({nowI, nowJ});
    qd.push(0);
    while (q.size()) {
      pair<long long int, long long int> topP = q.front();
      long long int nowD = qd.front();
      q.pop();
      qd.pop();
      for (long long int k = 0; k < 4; k++) {
        pair<long long int, long long int> nexP = topP;
        nexP.first += di[k];
        nexP.second += dj[k];
        if (dist[nowI][nowJ] + nowD < dist[nexP.first][nexP.second]) {
          dist[nexP.first][nexP.second] = dist[nowI][nowJ] + nowD;
          q.push(nexP);
          qd.push(nowD + has[nexP.first][nexP.second]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
