#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int dr8[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dc8[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dr4[] = {0, 1, -1, 0};
int dc4[] = {1, 0, 0, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int pos[n + 1][n + 1];
  pair<int, int> rc[n * n + 1];
  int in = 1;
  int l[n * n + 1], r[n * n + 1], u[n * n + 1], d[n * n + 1];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      pos[i][j] = in++;
      rc[in - 1] = {i, j};
      u[in - 1] = i - 1;
      d[in - 1] = n - i;
      r[in - 1] = n - j;
      l[in - 1] = j - 1;
    }
  }
  int x, an = 0;
  for (int i = 0; i < n * n; i++) {
    cin >> x;
    int row = rc[x].first, col = rc[x].second;
    an += min(l[x], min(r[x], min(u[x], d[x])));
    for (int i = 1; i < row; i++) {
      if (d[pos[i][col]] > 0) d[pos[i][col]]--;
    }
    for (int i = row + 1; i <= n; i++) {
      if (u[pos[i][col]] > 0) u[pos[i][col]]--;
    }
    for (int i = 1; i < col; i++) {
      if (r[pos[row][i]] > 0) r[pos[row][i]]--;
    }
    for (int i = col + 1; i <= n; i++) {
      if (l[pos[row][i]] > 0) l[pos[row][i]]--;
    }
  }
  cout << an << "\n";
}
