#include <bits/stdc++.h>
using namespace std;
const long long maxn = 505;
const long long mod = 1000000000;
const long double eps = 1e-9;
const long long inf = ((1ll << 31ll) - 1ll);
const long long INF = 10000000000000000ll;
const long double pi = acos(-1);
long long qpow(long long b, long long e) {
  if (!e) return 1;
  if (e & 1) return qpow(b, e - 1) * b;
  long long pwur = qpow(b, e >> 1);
  return pwur * pwur;
}
long long modinv(long long x) { return qpow(x, mod - 2); }
long long a[maxn][maxn], dist[maxn][maxn], id[maxn * maxn], n;
deque<pair<long long, long long> > q;
pair<long long, long long> mv[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
void bfs(long long u, long long v) {
  q.push_back({u, v});
  while (!q.empty()) {
    long long u = q.front().first;
    long long v = q.front().second;
    q.pop_front();
    for (long long i = 0; i < 4; i++) {
      if (u + mv[i].first <= 0 || u + mv[i].first >= n) continue;
      if (v + mv[i].second <= 0 || v + mv[i].second >= n) continue;
      if (dist[u + mv[i].first][v + mv[i].second] >
          dist[u][v] + (a[u + mv[i].first][v + mv[i].second] > a[u][v])) {
        dist[u + mv[i].first][v + mv[i].second] =
            dist[u][v] + (a[u + mv[i].first][v + mv[i].second] > a[u][v]);
        if (a[u + mv[i].first][v + mv[i].second] > a[u][v])
          ;
        else
          q.push_front({u + mv[i].first, v + mv[i].second});
      }
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(0);
  srand(time(NULL));
  cin >> n;
  for (long long i = 0; i <= n + 1; i++)
    for (long long j = 0; j <= n + 1; j++) {
      a[i][j] = mod;
      dist[i][j] = mod;
    }
  for (long long i = 1; i <= n * n; i++) {
    long long u;
    cin >> u;
    u--;
    a[u / n + 1][u % n + 1] = i;
    id[i] = u;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cout << a[i][j] << ' ';
      dist[i][j] = min(min(i, n - i + 1), min(j, n - j + 1));
    }
    cout << '\n';
  }
  long long ans = 0;
  for (long long i = 1; i <= n * n; i++) {
    long long u = id[i] / n + 1;
    long long v = id[i] % n + 1;
    dist[u][v]--;
    ans += dist[u][v];
    bfs(u, v);
  }
  cout << ans << '\n';
  return 0;
}
