#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}
void _cout() { cout << "\n"; }
template <class Head, class... Tail>
void _cout(Head&& head, Tail&&... tail) {
  cout << head;
  _cout(forward<Tail>(tail)...);
}
int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); }
template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p) {
  cout << "[" << p.first << ", " << p.second << "]";
  return os;
}
int main(void) {
  int n;
  _cin(n);
  vector<int> p(n * n);
  for (int i = 0; i < (n * n); i++) cin >> p[i];
  vector<vector<int>> s(n + 2, vector<int>(n + 2, 0));
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (n); j++) s[i + 1][j + 1] = 1;
  ll ans = 0;
  for (auto k : p) {
    int i = (k - 1) / n + 1, j = (k - 1) % n + 1;
    s[i][j] = 0;
    using P = pair<int, pair<int, int>>;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push({0, {i, j}});
    vector<vector<int>> c(n + 2, vector<int>(n + 2, 1e9));
    while (!q.empty()) {
      auto [cost, pos] = q.top();
      auto [i, j] = pos;
      q.pop();
      if (i == 0 && j == 0) {
        ans += cost;
        break;
      }
      if (i + 1 < n + 2)
        if (cost + s[i + 1][j] < c[i + 1][j])
          q.push({c[i + 1][j] = cost + s[i + 1][j], {i + 1, j}});
      if (i - 1 >= 0)
        if (cost + s[i - 1][j] < c[i - 1][j])
          q.push({c[i - 1][j] = cost + s[i - 1][j], {i - 1, j}});
      if (j + 1 < n + 2)
        if (cost + s[i][j + 1] < c[i][j + 1])
          q.push({c[i][j + 1] = cost + s[i][j + 1], {i, j + 1}});
      if (j - 1 >= 0)
        if (cost + s[i][j - 1] < c[i][j - 1])
          q.push({c[i][j - 1] = cost + s[i][j - 1], {i, j - 1}});
    }
  }
  _cout(ans);
}
