#include <bits/stdc++.h>
using namespace std;
int a[501][501];
int b[501];
int n;
int h, s;
int ans = 0;
int x, y;
int ss = 0;
int bfs(int i, int j) {
  int s1, s2, s3, s4;
  s1 = s2 = s3 = s4 = 0;
  x = i;
  y = j;
  while (x > 1) {
    x--;
    if (a[x][j] != 0) {
      s1++;
    }
  }
  x = i;
  y = j;
  while (x < n) {
    x++;
    if (a[x][j] != 0) {
      s2++;
    }
  }
  x = i;
  y = j;
  while (y > 1) {
    y--;
    if (a[i][y] != 0) {
      s3++;
    }
  }
  x = i;
  y = j;
  while (y < n) {
    y++;
    if (a[i][y] != 0) {
      s4++;
    }
  }
  int pd = min(s1, min(s2, min(s3, s4)));
  if (pd == 0) {
    return ss;
  } else {
    ss = max(ss, pd);
    if (pd == s1) return bfs(i - 1, j);
    if (pd == s2) return bfs(i + 1, j);
    if (pd == s3) return bfs(i, j - 1);
    if (pd == s4) return bfs(i, j + 1);
  }
}
int judh(int c) {
  if (c % n == 0) return n;
  return c % n;
}
int juds(int c) {
  int d = 0;
  do {
    d++;
  } while (d * n < c);
  return d;
}
int main() {
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  cin >> n;
  int o = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      a[i][j] = o;
      o++;
    }
  for (int i = 1; i <= n * n; i++) cin >> b[i];
  for (int i = 1; i <= n * n; i++) {
    s = judh(b[i]);
    h = juds(b[i]);
    ans += bfs(h, s);
    a[h][s] = 0;
    ss = 0;
  }
  cout << ans << endl;
}
