#include <bits/stdc++.h>
using namespace std;
int dat[501][501] = {};
int dat2[501][501] = {};
bool block[501][501] = {};
struct S {
  int x;
  int y;
  int v;
};
auto cmp = [](const S& lhs, const S& rhs) { return lhs.v > rhs.v; };
void add(int x, int y, int v, int n, int m, queue<S>& s) {
  if (x == -1 || x == n || y == -1 || y == n) return;
  if (v < dat[x][y]) {
    dat[x][y] = v;
    int nv = v;
    if (!block[x][y]) nv++;
    s.push({x, y, nv});
  }
}
void update(int x, int y, int v, int n, int m) {
  queue<S> search;
  search.push({x, y, v});
  while (!search.empty()) {
    S s = search.front();
    search.pop();
    int cv = s.v;
    add(s.x - 1, s.y, cv, n, m, search);
    add(s.x + 1, s.y, cv, n, m, search);
    add(s.x, s.y - 1, cv, n, m, search);
    add(s.x, s.y + 1, cv, n, m, search);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  for (int i = (int)(0); i < (int)(n); i++) {
    for (int j = (int)(0); j < (int)(n); j++) {
      int a = i;
      int b = n - i - 1;
      int c = j;
      int d = n - j - 1;
      dat[i][j] = min(min(a, b), min(c, d));
    }
  }
  int64_t ans = 0;
  for (int repeat_index = 0; repeat_index < (int)n * n; repeat_index++) {
    int m;
    cin >> m;
    m--;
    int x = m % n;
    int y = m / n;
    int v = dat[x][y];
    block[x][y] = true;
    update(x, y, v, n, m + 1);
    ans += v;
    cout << x << "  " << y << " " << v << " " << ans << endl;
  }
  cout << ans << endl;
  return 0;
}
