#include <bits/stdc++.h>
using namespace std;
const int N = 512;
int g[N][N], n;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
int get_idx(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > n) return 0;
  return (x - 1) * n + y;
}
pair<int, int> get_pos(int x) {
  if (x % n == 0) {
    return {x / n, n};
  } else {
    return {x / n + 1, x % n};
  }
}
int _01BFS(int x, int y) {
  map<int, int> mp;
  set<int> vis;
  deque<pair<int, int> > dq;
  dq.push_back({x, y});
  mp[get_idx(x, y)] = 0;
  while (!dq.empty()) {
    int cx = dq.front().first, cy = dq.front().second;
    dq.pop_front();
    if (cx < 1 || cx > n || cy < 1 || cy > n) {
      return mp[get_idx(cx, cy)];
    }
    if (vis.count(get_idx(cx, cy))) continue;
    vis.insert(get_idx(cx, cy));
    int dis = mp[get_idx(cx, cy)];
    for (int i = 0; i < 4; i++) {
      int nx = cx + dx[i], ny = cy + dy[i];
      if (mp.count(get_idx(nx, ny))) {
        if (dis + g[nx][ny] < mp[get_idx(nx, ny)]) {
          mp[get_idx(nx, ny)] = dis + g[nx][ny];
          dq.push_front({nx, ny});
        }
      } else {
        mp[get_idx(nx, ny)] = dis + g[nx][ny];
        dq.push_back({nx, ny});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  int m = n * n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      g[i][j] = 1;
    }
  }
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    pair<int, int> p = get_pos(x);
    g[p.first][p.second] = 0;
    int temp = _01BFS(p.first, p.second);
    ans += temp;
  }
  cout << ans;
  return 0;
}
