#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long power(long long first, unsigned long long second,
                unsigned long long m) {
  if (second == 0) return 1;
  long long p = power(first, second / 2, m) % m;
  p = (p * p) % m;
  return (second % 2 == 0) ? p : (first * p) % m;
}
long long modInverse(long long a, long long m) {
  long long g = gcd(a, m);
  { return power(a, m - 2, m); }
}
void solve() {
  long long n;
  cin >> n;
  long long k = 1;
  long long left[n + 2][n + 2], right[n + 2][n + 2], up[n + 2][n + 2],
      down[n + 2][n + 2];
  memset(left, 0, sizeof left);
  memset(right, 0, sizeof right);
  memset(up, 0, sizeof up);
  memset(down, 0, sizeof down);
  map<long long, pair<long long, long long> > m;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      m[k++] = make_pair(i, j);
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      left[i][j] = j - 1;
      right[i][j] = n - j;
      up[i][j] = j - 1;
      down[i][j] = n - j;
    }
  }
  long long first;
  long long ans = 0;
  for (long long i = 1; i <= n * n; i++) {
    cin >> first;
    long long temp = 2000000000000000005;
    long long row = m[first].first;
    long long col = m[first].second;
    if (left[row][col] < temp) {
      temp = left[row][col];
    }
    if (right[row][col] < temp) {
      temp = right[row][col];
    }
    if (up[col][row] < temp) {
      temp = up[col][row];
    }
    if (down[col][row] < temp) {
      temp = down[col][row];
    }
    if (temp < 0) {
      while (1) {
      }
    }
    ans += temp;
    for (long long i = 1; i <= col - 1; i++) {
      right[row][i]--;
    }
    for (long long i = 1 + col; i <= n; i++) left[row][i]--;
    for (long long i = row - 1; i >= 1; i--) down[col][i]--;
    for (long long i = row + 1; i <= n; i++) up[col][i]--;
  }
  cout << ans << "\n";
}
signed main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
