#include <bits/stdc++.h>
using namespace std;
template <class T>
long long size(const T& x) {
  return x.size();
}
template <class T>
T smod(T a, T b) {
  return (a % b + b) % b;
}
long long dist[2750];
long long vis[2750];
vector<pair<long long, long long> > edg[2750];
void dijkstra(int start) {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      pq;
  memset(dist, -1, sizeof(dist));
  memset(vis, 0, sizeof(vis));
  pq.push(pair<long long, long long>(0, start));
  dist[start] = 0;
  while (!pq.empty()) {
    pair<long long, long long> cur = pq.top();
    pq.pop();
    if (vis[cur.second]) {
      continue;
    }
    dist[cur.second] = cur.first;
    vis[cur.second] = true;
    for (long long i = (0); i < (edg[cur.second].size()); ++i) {
      pair<long long, long long> nxt = edg[cur.second][i];
      int weight = nxt.first;
      int vertex_index = nxt.second;
      if (!vis[vertex_index]) {
        pq.push(pair<long long, long long>(weight + cur.first, vertex_index));
      }
    }
  }
}
long long n;
long long dx[4] = {0, 1, 0, -1};
long long dy[4] = {1, 0, -1, 0};
void add_edges(int val, int at) {
  long long cur = at;
  long long x = at % (n + 2);
  long long y = at / (n + 2);
  for (long long d = (0); d < (4); ++d) {
    long long nx = x + dx[d];
    long long ny = y + dy[d];
    long long w = val;
    if (nx == n + 1 || nx == 0 || ny == n + 1 || ny == 0) w = 0;
    long long nxt = ny * (n + 2) + nx;
    edg[cur].push_back(pair<long long, long long>(w, nxt));
    edg[nxt].push_back(pair<long long, long long>(w, cur));
  }
}
void add_edges2(int val, int at) {
  long long cur = at;
  long long x = at % (n + 2);
  long long y = at / (n + 2);
  for (long long d = (0); d < (4); ++d) {
    long long nx = x + dx[d];
    long long ny = y + dy[d];
    long long w = val;
    if (nx == n + 1 || nx == 0 || ny == n + 1 || ny == 0) w = 0;
    long long nxt = ny * (n + 2) + nx;
    edg[nxt].push_back(pair<long long, long long>(w, cur));
  }
}
void solve() {
  cin >> n;
  for (long long i = (1); i < (n + 1); ++i) {
    for (long long j = (1); j < (n + 1); ++j) {
      long long cur = i * (n + 2) + j;
      add_edges(1, cur);
    }
  }
  vector<long long> P(n * n);
  for (long long i = (0); i < (n * n); ++i) {
    cin >> P[i];
    P[i]--;
    long long y = P[i] / n;
    long long x = P[i] % n;
    y++;
    x++;
    P[i] = y * (n + 2) + x;
  }
  vector<vector<bool> > grid(n + 2, vector<bool>(n + 2, 0));
  for (long long i = (1); i < (n + 1); ++i) {
    for (long long j = (1); j < (n + 1); ++j) {
      grid[i][j] = 1;
    }
  }
  long long ans = 0;
  for (long long i = (0); i < (n * n); ++i) {
    dijkstra(P[i]);
    long long mn = 100000000000;
    for (long long i = (0); i < (n + 2); ++i) {
      for (long long j = (0); j < (n + 2); ++j) {
        if ((i == 0 || i == n + 1) && (j == 0 || j == n + 1)) continue;
        if (i == 0 || j == 0 || i == n + 1 || j == n + 1) {
          mn = min(dist[j * (n + 2) + i], mn);
        }
      }
    }
    ans += mn;
    add_edges2(0, P[i]);
    long long y = P[i] / (n + 2);
    long long x = P[i] % (n + 2);
    grid[y][x] = 0;
  }
  cout << ans << endl;
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
  return 0;
}
