#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using pii = pair<long long, long long>;
using Graph = vector<vector<pii> >;
constexpr long long INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr long long mod1 = 1e9 + 7;
constexpr long long mod2 = 998244353;
constexpr long long dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
constexpr long long dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
template <class A, class B>
inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B>
inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
template <class A, class B>
inline A updiv(const A a, const B b) {
  return (a + b - 1) / b;
}
template <class A, class B>
inline A gcd(const A a, const B b) {
  return b ? gcd(b, a % b) : a;
}
template <class A, class B>
inline A lcm(const A a, const B b) {
  return a / gcd(a, b) * b;
}
template <class T>
using min_heap = priority_queue<T, vector<T>, greater<T> >;
template <class T>
using max_heap = priority_queue<T>;
template <class A, class B>
using umap = unordered_map<A, B>;
signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  long long n, cnt = 1;
  cin >> n;
  vector<long long> p(n * n);
  vector<pii> pos(n * n + 1);
  vector<vector<long long> > seat(n, vector<long long>(n, 1));
  for (long long i = 0; i < (n); ++i) {
    for (long long j = 0; j < (n); ++j) {
      pos[cnt] = {i, j};
      ++cnt;
    }
  }
  for (auto &&e : p) {
    cin >> e;
  }
  auto inside = [&n](long long y, long long x) {
    return (0 <= y && y < n && 0 <= x && x < n);
  };
  long long res = 0;
  for (auto &&e : p) {
    long long sy = pos[e].first;
    long long sx = pos[e].second;
    seat[sy][sx] = 0;
    vector<vector<long long> > dist(n, vector<long long>(n, INF));
    min_heap<tuple<long long, long long, long long> > que;
    dist[sy][sx] = 0;
    que.emplace(0, sy, sx);
    long long minc = INF;
    while (!que.empty()) {
      auto [c, y, x] = que.top();
      que.pop();
      if (dist[y][x] < c) continue;
      for (long long i = 0; i < (4); ++i) {
        long long ny = y + dy[i];
        long long nx = x + dx[i];
        if (inside(ny, nx)) {
          if (chmin(dist[ny][nx], dist[y][x] + seat[ny][nx])) {
            que.emplace(dist[ny][nx], ny, nx);
          }
        } else {
          chmin(minc, dist[y][x]);
        }
      }
    }
    res += minc;
  }
  cout << res << '\n';
  return 0;
}
