#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
vector<char*> tokenizer(const char* args) {
  char* token = new char[111];
  strcpy(token, args);
  token = strtok(token, ", ");
  vector<char*> v({token});
  while (token = strtok(NULL, ", ")) v.push_back(token);
  return reverse(v.begin(), v.end()), v;
}
void debugg(vector<char*> args) { cerr << "\b\b "; }
template <typename Head, typename... Tail>
void debugg(vector<char*> args, Head H, Tail... T) {
  debug() << " [" << args.back() << ": " << H << "] ";
  args.pop_back();
  debugg(args, T...);
}
long long mod = 998244353;
const int MX = 0x3f3f3f3f;
int mh[501][501], n, dis[501][501];
int vis[501][501], dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int dfs(int i, int j, int v) {
  if (i > n || i < 1 || j > n || j < 1) return 0;
  if (vis[i][j] == v) return dis[i][j];
  vis[i][j] = v;
  int p;
  dis[i][j] = MX;
  for (p = 0; p <= 3; p++) {
    int x = i + dx[p], y = j + dy[p];
    int temp = dfs(x, y, v);
    if (x <= n && x >= 1 && y <= n && y >= 1 && mh[x][y]) temp++;
    dis[i][j] = min(dis[i][j], temp);
  }
  return dis[i][j];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int i, temp, j, k, cnt = 0, v = 1;
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) mh[i][j] = 1;
  }
  long long ans = 0;
  for (i = 1; i <= n * n; i++) {
    cin >> temp;
    cnt = 0;
    for (j = 1; j <= n; j++) {
      for (k = 1; k <= n; k++) {
        cnt++;
        if (cnt == temp) {
          ans += dfs(j, k, v);
          mh[j][k] = 0;
          v++;
          break;
        }
      }
    }
  }
  cout << ans;
  return 0;
}
