#include <bits/stdc++.h>
using namespace std;
int b[250001], n, c[501][501], used[500][500];
void upd(int x, int y) {
  if (x - 1 >= 0 && c[x][y] + 1 - used[x][y] < c[x - 1][y]) {
    c[x - 1][y] = c[x][y] + 1 - used[x][y];
    upd(x - 1, y);
  }
  if (y - 1 >= 0 && c[x][y] + 1 - used[x][y] < c[x][y - 1]) {
    c[x][y - 1] = c[x][y] + 1 - used[x][y];
    upd(x, y - 1);
  }
  if (y + 1 < n && c[x][y] + 1 - used[x][y] < c[x][y + 1]) {
    c[x - 1][y] = c[x][y] + 1 - used[x][y];
    upd(x, y + 1);
  }
  if (x + 1 < n && c[x][y] + 1 - used[x][y] < c[x + 1][y]) {
    c[x + 1][y] = c[x][y] + 1 - used[x][y];
    upd(x + 1, y);
  }
}
int main() {
  cin >> n;
  int nn = pow(n, 2);
  for (int i = 1; i <= nn; i++) {
    int u = (i - 1) % n, v = (i - 1) / n;
    c[u][v] = min(min(u, n - 1 - u), min(v, n - 1 - v));
  }
  int ans = 0;
  for (int i = 1; i <= nn; i++) {
    int y;
    cin >> y;
    int u = (y - 1) % n, v = (y - 1) / n;
    ans += c[u][v];
    used[u][v] = 1;
    upd(u, v);
  }
  cout << ans;
  return 0;
}
