#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using Piii = pair<int, pii>;
using pll = pair<ll, ll>;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);
inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream& operator<<(ostream& os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream& operator<<(ostream& os, const map<T, S> p) {
  for (auto el : p) cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream& operator<<(ostream& os, const vector<T>& v) {
  for (auto el : v) cout << el << " ";
  return os;
}
template <typename T>
inline ostream& operator<<(ostream& os, const deque<T>& v) {
  for (auto el : v) cout << el << " ";
  return os;
}
template <typename T>
inline ostream& operator<<(ostream& os, const set<T>& v) {
  for (auto el : v) cout << el << " ";
  return os;
}
template <typename T>
inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto& elem : ret) cin >> elem;
  return ret;
}
int N;
vector<int> P;
void input() {
  fast_io();
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;
    P.push_back(p);
  }
}
vector<vector<int>> dp;
int dijkstra(int x, int y, vector<string> s) {
  auto cmp = [](Piii a, Piii b) { return a.first > b.first; };
  priority_queue<Piii, vector<Piii>, decltype(cmp)> pq(cmp);
  vector<vector<int>> dist(N, vector<int>(N, 1000));
  dist[x][y] = 0;
  pq.emplace(0, make_pair(x, y));
  while (!pq.empty()) {
    int cost;
    pii cur;
    tie(cost, cur) = pq.top();
    pq.pop();
    if (cur.first == 0 || cur.first == N - 1 || cur.second == 0 ||
        cur.second == N - 1)
      return cost;
    if (dist[cur.first][cur.second] < cost) continue;
    for (int k = 0; k < 4; k++) {
      int nx = cur.first + dx[k];
      int ny = cur.second + dy[k];
      int nc = cost;
      if (nx < 0 || nx >= N) continue;
      if (ny < 0 || ny >= N) continue;
      if (dist[nx][ny] <= nc) continue;
      if (s[nx][ny] == '#') {
        dist[nx][ny] = dp[nx][ny] + 1;
        pq.emplace(dp[nx][ny] + 1, make_pair(0, 0));
      } else {
        dist[nx][ny] = nc;
        pq.emplace(nc, make_pair(nx, ny));
      }
    }
  }
  return dist[0][0];
}
int solve() {
  string init(N, '.');
  vector<string> S(N, init);
  reverse(P.begin(), P.end());
  int ans = 0;
  dp.assign(N, vector<int>(N, 5000));
  for (int i = 0; i < N * N; i++) {
    int x = P[i] / N;
    int y = P[i] % N;
    dp[x][y] = dijkstra(x, y, S);
    ans += dp[x][y];
    S[x][y] = '#';
  }
  cout << ans << endl;
  return 0;
}
int main() {
  input();
  solve();
  return 0;
}
