#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long longinf = 1LL << 60;
const int mx = 100010;
const long long mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
vector<vector<int>> da(500, vector<int>(500, 0));
int n;
int bfs(int s, int used[500][500]) {
  used[s / n][s % n] = 0;
  queue<pair<int, int>> q;
  q.push({s / n, s % n});
  int res = inf;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    for (int i = (int)(0); i < (int)(4); ++i) {
      int nx = p.first + dx[i];
      int ny = p.second + dy[i];
      if (nx < 0 || n <= nx || ny < 0 || n <= ny) {
        res = min(res, used[p.first][p.second]);
        if (res == 0) {
          return 0;
        }
        continue;
      }
      if (used[nx][ny] <= used[p.first][p.second] + (da[nx][ny] == 1 ? 1 : 0)) {
        continue;
      }
      used[nx][ny] = used[p.first][p.second] + (da[nx][ny] == 1 ? 1 : 0);
      q.push({nx, ny});
    }
  }
  return res;
}
int main() {
  for (int i = (int)(0); i < (int)(10000); ++i) {
    cout << i + 1 << " ";
  }
  return 0;
  cin >> n;
  vector<int> p(n * n);
  for (int i = (int)(0); i < (int)(n * n); ++i) {
    cin >> p[i];
    p[i]--;
  }
  long long ans = 0;
  for (int i = (int)(0); i < (int)(n * n); ++i) {
    int used[500][500];
    for (int i = (int)(0); i < (int)(500); ++i)
      for (int j = (int)(0); j < (int)(500); ++j) used[i][j] = inf;
    ans += bfs(p[n * n - 1 - i], used);
    da[p[n * n - 1 - i] / n][p[n * n - 1 - i] % n] = 1;
  }
  cout << ans << endl;
  return 0;
}
