#include <bits/stdc++.h>
using namespace std;
const int maxN = 501;
const int maxN2 = maxN * maxN;
bool dp[maxN][maxN];
map<int, pair<int, int> > mp;
int a[maxN2];
int n;
long long up[maxN][maxN], down[maxN][maxN], left1[maxN][maxN],
    right1[maxN][maxN];
map<int, pair<int, int> > refer;
void init() {
  int k = 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      refer[k] = {i, j};
      k++;
    }
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      up[i][j] = i - 1;
      down[i][j] = n - i;
      left1[i][j] = j - 1;
      right1[i][j] = n - j;
    }
  }
}
void updateAll(int x, int y) {
  for (int i = y + 1; i <= n; i++) left1[x][i] -= 1;
  for (int i = y - 1; i >= 1; i--) right1[x][i] -= 1;
  for (int i = x - 1; i >= 1; i--) down[i][y] -= 1;
  for (int i = x + 1; i <= n; i++) up[i][y] -= 1;
  left1[x][y] -= 1, right1[x][y] -= 1, up[x][y] -= 1, down[x][y] -= 1;
}
long long findmin(long long a, long long b, long long c, long long d) {
  return min(a, min(b, min(c, d)));
}
long long solve() {
  long long ans = 0;
  for (int i = 1; i <= n * n; i++) {
    pair<int, int> temp = refer[a[i]];
    int x = temp.first;
    int y = temp.second;
    if (x == 1 || x == n || y == 1 || y == n) {
      updateAll(x, y);
    } else {
      long long a = up[x][y], b = down[x][y], c = left1[x][y], d = right1[x][y];
      long long mini = findmin(a, b, c, d);
      ans += mini;
      updateAll(x, y);
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  int val;
  init();
  int k = 1;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> val;
      mp[val] = {i, j};
      a[k] = val;
      k++;
    }
  }
  cout << solve() << '\n';
  return 0;
}
