#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
const int MAX = 505;
int N;
int qs[MAX * MAX];
int ans[MAX][MAX];
set<pii> neighs[MAX][MAX];
pii ToPosition(int p) {
  p--;
  return {p / N, p % N};
}
int ToIndex(int x, int y) { return x * N + y; }
bool Okay(int x, int y) { return x >= 0 && x < N && y >= 0 && y < N; }
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
unordered_set<int> bio;
void dfs(int x, int y) {
  for (auto it = neighs[x][y].begin(); it != neighs[x][y].end(); it++) {
    int nx = it->first;
    int ny = it->second;
    if (!Okay(nx, ny)) continue;
    if (bio.find(ToIndex(nx, ny)) != bio.end()) continue;
    if (ans[nx][ny] != ans[x][y] + 1) continue;
    bio.insert(ToIndex(nx, ny));
    dfs(nx, ny);
  }
  ans[x][y]--;
}
void Update(int x, int y) {
  bio.clear();
  dfs(x, y);
  ans[x][y] = 0;
  for (auto it = neighs[x][y].begin(); it != neighs[x][y].end(); it++) {
    for (auto it2 = neighs[x][y].begin(); it2 != neighs[x][y].end(); it2++) {
      if (it->first == it2->first && it->second != it2->second) {
        neighs[it->first][it->second].erase(make_pair(x, y));
        neighs[it2->first][it2->second].erase(make_pair(x, y));
        neighs[it->first][it->second].insert(
            make_pair(it2->first, it2->second));
        neighs[it2->first][it2->second].insert(
            make_pair(it->first, it->second));
      } else if (it->first != it2->first && it->second == it2->second) {
        neighs[it->first][it->second].erase(make_pair(x, y));
        neighs[it2->first][it2->second].erase(make_pair(x, y));
        neighs[it->first][it->second].insert(
            make_pair(it2->first, it2->second));
        neighs[it2->first][it2->second].insert(
            make_pair(it->first, it->second));
      }
    }
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    scanf("%d", qs + i);
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ans[i][j] = min(min(i, j), min(N - 1 - i, N - 1 - j));
      for (int dd = 0; dd < 4; dd++) {
        int nx = dx[dd] + i;
        int ny = dy[dd] + j;
        if (!Okay(nx, ny)) continue;
        neighs[i][j].insert({nx, ny});
      }
    }
  }
  ll sum = 0;
  for (int i = 0; i < N * N; i++) {
    pii pos = ToPosition(qs[i]);
    sum += ans[pos.first][pos.second];
    Update(pos.first, pos.second);
  }
  cout << sum << endl;
  return 0;
}
