#include <bits/stdc++.h>
using namespace std;
bool isExit(int i, int j, int n) {
  if (i == 0 || i == n + 1 || j == 0 || j == n + 1) {
    return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int p[n * n];
  int ord[n * n + 1];
  for (int i = 0; i < n * n; i++) {
    cin >> p[i];
    ord[p[i]] = i + 1;
  }
  vector<pair<int, int>> index(n * n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      index[(i - 1) * n + j] = {i, j};
    }
  }
  int ans = 0;
  int dx[4] = {0, 0, -1, 1};
  int dy[4] = {-1, 1, 0, 0};
  for (int i = 0; i < n * n; i++) {
    int curr = p[i];
    deque<pair<int, pair<int, int>>> dq;
    dq.push_back({0, index[curr]});
    bool vis[n + 1][n + 1];
    memset(vis, false, sizeof vis);
    int dist[n + 1][n + 1];
    memset(dist, 0, sizeof dist);
    dist[index[curr].first][index[curr].second] = 0;
    while (!dq.empty()) {
      pair<int, pair<int, int>> now = dq.front();
      dq.pop_front();
      vis[now.second.first][now.second.second] = true;
      bool isend = false;
      for (int j = 0; j < 4; j++) {
        if (isExit(now.second.first + dx[j], now.second.second + dy[j], n)) {
          isend = true;
          break;
        }
      }
      if (isend) {
        ans += dist[now.second.first][now.second.second];
        break;
      }
      for (int j = 0; j < 4; j++) {
        int goi = now.second.first + dx[j], goj = now.second.second + dy[j];
        int goNum = (goi - 1) * n + goj;
        if (!vis[goi][goj]) {
          if (ord[goNum] > ord[curr]) {
            dist[goi][goj] = 1 + dist[now.second.first][now.second.second];
            dq.push_back({1, {goi, goj}});
          } else {
            dist[goi][goj] = dist[now.second.first][now.second.second];
            dq.push_front({0, {goi, goj}});
          }
        }
      }
    }
  }
  cout << ans;
}
