#include <bits/stdc++.h>
using namespace std;
int pos(int n, int x) {
  int l, u;
  l = x % n;
  u = (x + n - 1) / n;
  if (l > n - l) l = n - l;
  if (u > n - u) u = n - u;
  if (l > u)
    return u;
  else
    return l;
}
int search(vector<int> s, int n, int x) {
  int y = x, start, strain = 0, minpos, j, c = 0;
  while ((y % n != 0) && (y % n != 1) && y >= n && y < n * (n - 1)) {
    if (s[y + 1] != 1) break;
    y++;
  }
  start = y;
  minpos = pos(n, start);
  while ((y % n != 0) && (y % n != 1) && y >= n && y < n * (n - 1)) {
    switch (strain) {
      case 0:
        if (s[y + 1] != 0) {
          y += 1;
          strain = 3;
        } else if (s[y - n] != 0)
          y -= n;
        else if (s[y - 1] != 0) {
          strain = 1;
        } else if (s[y + n] != 0) {
          y += n;
          strain = 2;
        } else {
          return pos(n, y);
        }
        break;
      case 1:
        if (s[y - n] != 0) {
          y -= n;
          strain = 0;
        } else if (s[y - 1] != 0)
          y -= 1;
        else if (s[y + n] != 0) {
          y += n;
          strain = 2;
        } else if (s[y + 1] != 0) {
          y += 1;
          strain = 3;
        } else {
          return pos(n, y);
        }
        break;
      case 2:
        if (s[y - 1] != 0) {
          y -= 1;
          strain = 1;
        } else if (s[y + n] != 0)
          y += n;
        else if (s[y + 1] != 0) {
          y += 1;
          strain = 3;
        } else if (s[y - n] != 0) {
          y -= n;
          strain = 0;
        } else {
          return pos(n, y);
        }
        break;
      case 3:
        if (s[y + n] != 0) {
          y += n;
          strain = 2;
        } else if (s[y + 1] != 0)
          y += 1;
        else if (s[y - n] != 0) {
          y -= n;
          strain = 0;
        } else if (s[y - 1] != 0) {
          y -= 1;
          strain = 1;
        } else {
          return pos(n, y);
        }
        break;
    }
    if ((j = pos(n, y)) < minpos) minpos = j;
    if (y == start) c++;
    if (c >= 4) {
      return minpos;
    }
  }
  return 0;
}
int main() {
  int n, x, count = 0;
  cin >> n;
  vector<int> s(n * n + 1);
  for (int i = 0; i < n * n; i++) {
    cin >> x;
    s[x] = 1;
    count += search(s, n, x);
  }
  cout << count << endl;
  return 0;
}
