#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr long long INF = (1ll << 60);
constexpr long long MOD = 1000000007ll;
using graph = vector<vector<long long>>;
struct Edge {
  long long to;
  long long cost;
  Edge(long long t, long long c) : to(t), cost(c) {}
};
long long dx[] = {1, 0, -1, 0};
long long dy[] = {0, -1, 0, 1};
using meme = pair<long long, pair<long long, long long>>;
bool operator>(const meme a, const meme b) {
  if (a.first > b.first) {
    return true;
  } else if (a.first == b.first) {
    return a.second > b.second;
  } else {
    return false;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n;
  cin >> n;
  vector<long long> p(n * n);
  vector<pair<long long, long long>> xy(n * n);
  for (long long i = (0); i < (n * n); i++) {
    cin >> p[i];
    p[i]--;
    xy[i] = make_pair(p[i] / n, p[i] % n);
  }
  vector<vector<bool>> isstay(n, vector<bool>(n, true));
  long long ans = 0;
  vector<vector<bool>> seen(n, vector<bool>(n, false));
  auto comp = [](meme a, meme b) { return (a.first > b.first); };
  for (long long i = (0); i < (n * n); i++) {
    seen = vector<vector<bool>>(n, vector<bool>(n));
    priority_queue<meme, vector<meme>, decltype(comp)> que{comp};
    que.push(make_pair(0LL, xy[i]));
    seen[xy[i].first][xy[i].second] = true;
    while (!que.empty()) {
      auto data = que.top();
      que.pop();
      long long x = data.second.first, y = data.second.second;
      seen[x][y] = true;
      if (x <= 0 || x >= n - 1 || y <= 0 || y >= n - 1) {
        ans += data.first;
        break;
      }
      for (long long j = (0); j < (4); j++) {
        if (seen[x + dx[j]][y + dy[j]] == true) continue;
        if (isstay[x + dx[j]][y + dy[j]] == true) {
          que.push(make_pair(data.first + 1, make_pair(x + dx[j], y + dy[j])));
        } else {
          que.push(make_pair(data.first, make_pair(x + dx[j], y + dy[j])));
        }
      }
    }
    isstay[xy[i].first][xy[i].second] = false;
  }
  cout << ans << "\n";
  return 0;
}
