#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
void go() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long int N = 1e3 + 5;
long long int arr[N][N];
bool vis[N][N];
priority_queue<
    pair<long long int, pair<long long int, long long int> >,
    vector<pair<long long int, pair<long long int, long long int> > >,
    greater<pair<long long int, pair<long long int, long long int> > > >
    q;
map<pair<long long int, long long int>, long long int> mp;
int main() {
  long long int n;
  cin >> n;
  long long int p = 1;
  for (int i = (int)(1); i <= (int)(n); i++) {
    for (int j = (int)(1); j <= (int)(n); j++) {
      arr[i][j] = p++;
      vis[i][j] = false;
    }
  }
  long long int ans = 0;
  for (int i = (int)(1); i <= (int)(n * n); i++) {
    long long int a;
    cin >> a;
    long long int b = a % n;
    long long int c = a / n;
    long long int x, y;
    if (b == 0) {
      x = c;
      y = n;
    } else {
      x = c + 1;
      y = b;
    }
    vector<long long int> res;
    q = priority_queue<
        pair<long long int, pair<long long int, long long int> >,
        vector<pair<long long int, pair<long long int, long long int> > >,
        greater<pair<long long int, pair<long long int, long long int> > > >();
    q.push(make_pair(0, make_pair(x, y)));
    long long int sol = 0;
    mp[make_pair(x, y)] = 1;
    while (!q.empty()) {
      long long int x1 = q.top().second.first;
      long long int x2 = q.top().second.second;
      long long int x3 = q.top().first;
      q.pop();
      if (x1 % n == 1 || x2 % n == 1 || x1 % n == 0 || x2 % n == 0) {
        res.push_back(x3);
        break;
      }
      if (x1 - 1 >= 1 && !mp[make_pair(x1 - 1, x2)]) {
        if (!vis[x1 - 1][x2])
          q.push(make_pair(x3 + 1, make_pair(x1 - 1, x2)));
        else
          q.push(make_pair(x3, make_pair(x1 - 1, x2)));
        mp[make_pair(x1 - 1, x2)] = 1;
      }
      if (x1 + 1 <= n && !mp[make_pair(x1 + 1, x2)]) {
        if (!vis[x1 + 1][x2])
          q.push(make_pair(x3 + 1, make_pair(x1 + 1, x2)));
        else
          q.push(make_pair(x3, make_pair(x1 + 1, x2)));
        mp[make_pair(x1 + 1, x2)] = 1;
      }
      if (x2 - 1 >= 1 && !mp[make_pair(x1, x2 - 1)]) {
        if (!vis[x1][x2 - 1])
          q.push(make_pair(x3 + 1, make_pair(x1, x2 - 1)));
        else
          q.push(make_pair(x3, make_pair(x1, x2 - 1)));
        mp[make_pair(x1, x2 - 1)] = 1;
      }
      if (x2 + 1 <= n && mp[make_pair(x1, x2 + 1)]) {
        if (!vis[x1][x2 + 1])
          q.push(make_pair(x3 + 1, make_pair(x1, x2 + 1)));
        else
          q.push(make_pair(x3, make_pair(x1, x2 + 1)));
        mp[make_pair(x1, x2 + 1)] = 1;
      }
    }
    vis[x][y] = true;
    ans += res[0];
    mp.clear();
  }
  cout << ans;
  return 0;
}
