#include <bits/stdc++.h>
using namespace std;
int n, x, l, c, s;
bool a[502][502], b[502][502];
struct str {
  int x, y, c;
};
int di[] = {0, 0, 1, -1};
int dj[] = {1, -1, 0, 0};
queue<str> q;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) a[i][j] = 1;
  for (int k = 1; k <= n * n; k++) {
    cin >> x;
    l = (x - 1) / n + 1;
    c = x % n;
    if (c == 0) c = n;
    a[l][c] = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) b[i][j] = 0;
    while (!q.empty()) q.pop();
    q.push({l, c, 0});
    b[l][c] = 1;
    int mini = n;
    while (!q.empty()) {
      int i = q.front().x;
      int j = q.front().y;
      int cost = q.front().c;
      q.pop();
      for (int d = 0; d < 4; d++) {
        int i2 = i + di[d];
        int j2 = j + dj[d];
        int c2 = cost + a[i2][j2];
        if (i2 < 1 || j2 < 1 || i2 > n || j2 > n)
          mini = min(mini, cost);
        else if (!b[i2][j2])
          b[i2][j2] = 1, q.push({i2, j2, c2});
      }
    }
    s += mini;
  }
  cout << s << '\n';
  return 0;
}
