#include <bits/stdc++.h>
using namespace std;
const int N = 505, dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int n, p[N * N], a[N][N], dis[N * N];
pair<int, int> q[N * N];
bool vis[N][N];
long long ans;
inline pair<int, int> trs(const int& x) {
  if (x % n) return make_pair(x / n + 1, x % n);
  return make_pair(x / n, n);
}
inline int BFS(pair<int, int> st, int ret = 1e9) {
  register int H = 0, T = 1, i;
  q[vis[st.first][st.second] = 1] = st;
  dis[1] = a[st.first][st.second] = 0;
  while (H < T) {
    pair<int, int> nw = q[++H];
    if (nw.first == 1 || nw.first == n || nw.second == 1 || nw.second == n) {
      ret = min(ret, dis[H]);
      continue;
    }
    for (i = 0; i < 4; ++i) {
      pair<int, int> to = make_pair(nw.first + dx[i], nw.second + dy[i]);
      if (!vis[to.first][to.second])
        q[++T] = to, vis[to.first][to.second] = 1,
        dis[T] = dis[H] + a[to.first][to.second];
    }
  }
  for (i = 1; i <= T; ++i) vis[q[i].first][q[i].second] = 0;
  return ret;
}
int main() {
  register int i, j;
  for (scanf("%d", &n), i = 1; i <= n * n; ++i) scanf("%d", &p[i]);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= n; ++j) a[i][j] = 1;
  for (i = 1; i <= n * n; ++i) ans += BFS(trs(p[i]));
  return printf("%lld", ans), 0;
}
