#include <bits/stdc++.h>
using namespace std;
template <class t, class u>
bool chmax(t& a, u b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u>
bool chmin(t& a, u b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class t, class u>
t MAX(t a, u b) {
  if (a >= b) return a;
  return b;
}
template <class t, class u>
t MIN(t a, u b) {
  if (a <= b) return a;
  return b;
}
template <class t>
int SIZE(t s) {
  return (int)s.size();
}
template <class t>
using vc = vector<t>;
template <class t>
using vvc = vc<vc<t>>;
using vvi = vc<vc<int>>;
using pi = pair<int, int>;
using vi = vc<int>;
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
const long long INF = 1LL << 60;
int n;
vvc<bool> lea;
vvi dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vvc<bool> thr;
int bfs(int x, int y) {
  queue<pi> q;
  q.push(make_pair(x, y));
  vvi dis(507, vi(507, -1));
  dis[x][y] = 0;
  int ans = 1e9;
  while (!q.empty()) {
    pi num = q.front();
    q.pop();
    int x = num.first, y = num.second;
    for (int i = int(0); i < int(4); i++) {
      int xx = x + dir[i][0], yy = y + dir[i][1];
      if (xx < 0 || xx > n - 1 || yy < 0 || yy > n - 1) {
        chmin(ans, dis[x][y]);
        if (dis[x][y] == 0) thr[x][y] = true;
        continue;
      }
      if (thr[xx][yy]) {
        chmin(ans, dis[x][y]);
        if (dis[x][y] == 0) thr[x][y] = true;
        continue;
      }
      if (dis[xx][yy] != -1) continue;
      dis[xx][yy] = dis[x][y];
      if (!lea[xx][yy]) dis[xx][yy]++;
      q.push(make_pair(xx, yy));
    }
  }
  return ans;
}
int main() {
  cout << fixed << setprecision(20);
  cin >> n;
  lea.resize(507, vc<bool>(507, false));
  thr.resize(507, vc<bool>(507, false));
  int ans = 0;
  for (int i = int(0); i < int(n * n); i++) {
    int a;
    cin >> a;
    a--;
    int x, y;
    y = a / n;
    x = a % n;
    if (x == 0 || x == n - 1 || y == 0 || y == n - 1) {
      thr[x][y] = true;
      lea[x][y] = true;
      continue;
    }
    ans += bfs(x, y);
    lea[x][y] = true;
  }
  cout << ans << endl;
  return 0;
}
