#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;
int main() {
  int n;
  vector<int> p;
  cin >> n;
  for (int i = 0; i < n * n; i++) {
    int j;
    cin >> j;
    p.push_back(j);
  }
  long long mp[n + 2][n + 2];
  for (int i = 0; i <= n + 1; i++) {
    for (int j = 0; j <= n + 1; j++) {
      mp[i][j] = 1;
    }
  }
  for (int i = 0; i <= n + 1; i++) {
    mp[i][0] = 0;
    mp[i][n + 1] = 0;
    mp[0][i] = 0;
    mp[n + 1][i] = 0;
  }
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    priority_queue<pair<int, pair<int, int> >,
                   vector<pair<int, pair<int, int> > >,
                   greater<pair<int, pair<int, int> > > >
        pq;
    int x = p[i] % n;
    int y = p[i] / n;
    if (x == 0) {
      y--;
      x = n;
    }
    y++;
    auto st = make_pair(x, y);
    pq.push(make_pair(0, st));
    map<pair<int, int>, int> ev;
    long long t_ans = 0;
    int dirx[] = {1, 0, -1, 0};
    int diry[] = {0, 1, 0, -1};
    while (pq.size() > 0) {
      auto tmp = pq.top();
      pq.pop();
      auto w = tmp.first;
      auto pt = tmp.second;
      if (ev.count(pt)) continue;
      ev[pt] = 1;
      if (pt.first == 0 || pt.first == n + 1 || pt.second == 0 ||
          pt.second == n + 1) {
        t_ans = w;
        break;
      }
      for (int j = 0; j < 4; j++) {
        auto next = make_pair(pt.second + diry[j], pt.first + dirx[j]);
        if (ev.count(next)) continue;
        if (mp[pt.second + diry[j]][pt.first + dirx[j]] == 1) {
          pq.push(make_pair(w + 1, next));
        } else {
          pq.push(make_pair(w, next));
        }
      }
    }
    mp[y][x] = 0;
    ans += t_ans;
  }
  cout << ans << endl;
  return 0;
}
