#include <bits/stdc++.h>
using namespace std;
struct piii {
  long long first, second, th;
  bool operator<(const piii &b) {
    if (first != b.first) return first < b.first;
    if (second != b.second) return second < b.second;
    return th < b.th;
  }
};
int n, p, res;
deque<pair<long long, long long> > de;
int d[505][505];
bool dd[505][505], isout;
void go(int r, int c, int val) {
  if (isout) return;
  if (r < 1 || c < 1 || r > n || c > n) {
    isout = true;
    res += val;
    return;
  }
  if (d[r][c] <= val) return;
  if (dd[r][c]) {
    d[r][c] = val;
    de.push_front(pair<long long, long long>({r, c}));
  } else {
    d[r][c] = val + 1;
    de.push_back(pair<long long, long long>({r, c}));
  }
}
int bfs(int r, int c) {
  dd[r][c] = true;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) d[i][j] = 1e9;
  d[r][c] = 0;
  de.clear();
  de.push_back(pair<long long, long long>({r, c}));
  isout = false;
  while (!isout && !de.empty()) {
    r = de.front().first, c = de.front().second;
    de.pop_front();
    go(r + 1, c, d[r][c]);
    go(r, c + 1, d[r][c]);
    go(r - 1, c, d[r][c]);
    go(r, c - 1, d[r][c]);
  }
}
int main() {
  cout << setprecision(2) << fixed;
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    cin >> p;
    bfs((p - 1) / n + 1, (p - 1) % n + 1);
  }
  cout << res;
  return 0;
}
