#include <bits/stdc++.h>
using namespace std;
int da[4] = {0, 1, 0, -1}, db[4] = {1, 0, -1, 0};
int ans[505][505];
bool seats[505][505];
queue<pair<int, int> > bfs;
int n, p, curr_ans, final_ans;
int main() {
  scanf("%d", &n);
  int n2 = n * n;
  memset(seats, true, sizeof seats);
  final_ans = 0;
  for (int i = 0; i < n2; ++i) {
    scanf("%d", &p);
    int a = (p - 1) / n, b = (p - 1) % n;
    ans[a][b] = 0;
    seats[a][b] = false;
    bfs.push(pair<int, int>(a, b));
    curr_ans = 1023456789;
    while (!bfs.empty()) {
      int ua, ub;
      tie(ua, ub) = bfs.front();
      bfs.pop();
      if (ua == 0 || ua == n - 1 || ub == 0 || ub == n - 1)
        curr_ans = min(curr_ans, ans[ua][ub]);
      for (int i = 0; i < 4; ++i) {
        int na = ua + da[i], nb = ub + db[i];
        if (na < 0 || na >= n || nb < 0 || nb >= n) continue;
        if (abs(na - a) < abs(ua - a) || abs(nb - b) < abs(ub - b)) continue;
        ans[na][nb] = ans[ua][ub] + (int)seats[na][nb];
        bfs.push(pair<int, int>(na, nb));
      }
    }
    final_ans += curr_ans;
  }
  printf("%d", final_ans);
  return 0;
}
