#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
void inp_out() {}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  inp_out();
  long long int n;
  cin >> n;
  vector<vector<long long int> > adj(n * n);
  for (long long int i = 0; i < n; ++i) {
    for (long long int j = 0; j < n; ++j) {
      if (i > 0) adj[n * i + j].push_back(n * (i - 1) + j);
      if (j > 0) adj[n * i + j].push_back(n * i + j - 1);
      if (i < n - 1) adj[n * i + j].push_back(n * (i + 1) + j);
      if (j < n - 1) adj[n * i + j].push_back(n * i + j + 1);
    }
  }
  vector<long long int> a(n * n, 1);
  long long int ans = 0;
  for (long long int i = 0; i < n * n; ++i) {
    long long int x;
    cin >> x;
    --x;
    vector<long long int> dp(n * n);
    vector<long long int> vis(n * n);
    queue<long long int> q;
    q.push(x);
    vis[x] = 1;
    while (!q.empty()) {
      long long int val = q.front();
      q.pop();
      for (auto i : adj[val]) {
        if (!vis[i]) {
          vis[i] = 1;
          q.push(i);
          dp[i] = dp[val] + a[i];
        }
      }
    }
    a[x] = 0;
    long long int temp = 3e18;
    for (long long int i = 0; i < n; ++i) temp = min(temp, dp[i]);
    for (long long int i = 0; i < n; ++i) temp = min(temp, dp[(n - 1) * n + i]);
    for (long long int i = 0; i < n; ++i) temp = min(temp, dp[i * n]);
    for (long long int i = 0; i < n; ++i) temp = min(temp, dp[i * n + (n - 1)]);
    ans += temp;
  }
  cout << ans;
  return 0;
}
