#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using vvc = vector<vector<char>>;
using ll = long long int;
ll dfs(ll N, ll x, ll y, const vvb &map, vvll &dp, vvb &ald) {
  ald[x][y] = true;
  if (dp[x][y] > -1) {
    return dp[x][y];
  }
  if (x == 0 || x == N - 1) {
    return dp[x][y] = 0;
  }
  if (y == 0 || y == N - 1) {
    return dp[x][y] = 0;
  }
  ll p = 1000000000;
  if (0 <= x - 1 && ald[x - 1][y] == false) {
    ll h;
    dp[x - 1][y] = dfs(N, x - 1, y, map, dp, ald);
    h = dp[x - 1][y];
    if (map[x - 1][y] == true) {
      h++;
    }
    p = min(p, h);
  }
  if (0 <= y - 1 && ald[x][y - 1] == false) {
    ll i;
    dp[x][y - 1] = dfs(N, x, y - 1, map, dp, ald);
    i = dp[x][y - 1];
    if (map[x][y - 1] == true) {
      i++;
    }
    p = min(p, i);
  }
  if (x + 1 < N && ald[x + 1][y] == false) {
    ll j;
    dp[x + 1][y] = dfs(N, x + 1, y, map, dp, ald);
    j = dp[x + 1][y];
    if (map[x + 1][y] == true) {
      j++;
    }
    p = min(p, j);
  }
  if (y + 1 < N && ald[x][y + 1] == false) {
    ll k;
    dp[x][y + 1] = dfs(N, x, y + 1, map, dp, ald);
    k = dp[x][y + 1];
    if (map[x][y + 1] == true) {
      k++;
    }
    p = min(p, k);
  }
  dp[x][y] = p;
  return dp[x][y];
}
int main() {
  ll N;
  cin >> N;
  vll p(N * N);
  for (ll i = 0; i < N * N; i++) {
    cin >> p[i];
  }
  vvb map(N, vb(N, true));
  ll c = 0;
  for (ll i = 0; i < N * N; i++) {
    vvll dp(N, vll(N, -10));
    vvb ald(N, vb(N, false));
    ll k = p[i];
    ll m = k % N;
    ll x, y;
    if (m == 0) {
      x = N - 1;
      y = k / N - 1;
    } else {
      x = k % N - 1;
      y = k / N;
    }
    dfs(N, x, y, map, dp, ald);
    map[x][y] = false;
    c += dp[x][y];
  }
  cout << (c);
}
