#include <bits/stdc++.h>
using namespace std;
const int asize = 505;
int n, m[asize][asize], x[4] = {-1, 0, 1, 0}, y[4] = {0, 1, 0, -1};
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL);
  int k, n2, r, c, v;
  cin >> n;
  n2 = n * n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) m[i][j] = 1e9;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      m[i][j] = min(m[i][j], i - 1);
      m[i][j] = min(m[i][j], j - 1);
      m[i][j] = min(m[i][j], n - i);
      m[i][j] = min(m[i][j], n - j);
    }
  }
  int sum = 0;
  for (int i = 0; i < n2; i++) {
    cin >> k;
    r = ((k - 1) / n) + 1;
    c = k % n;
    if (c == 0) c = n;
    sum += m[r][c];
    queue<pair<pair<int, int>, int> > q;
    q.emplace(make_pair(r, c), m[r][c]);
    while (!q.empty()) {
      r = q.front().first.first, c = q.front().first.second,
      v = q.front().second;
      q.pop();
      for (int j = 0; j < 4; j++) {
        if (r + x[j] < 1 || r + x[j] > n || c + y[j] < 1 || c + y[j] > n)
          continue;
        if (m[r + x[j]][c + y[j]] > v) {
          m[r + x[j]][c + y[j]] = v;
          q.emplace(make_pair(r + x[j], c + y[j]), v + 1);
        }
      }
    }
  }
  cout << sum;
  return 0;
}
