#include <bits/stdc++.h>
using ll = long long;
using Pr = std::pair<ll, ll>;
constexpr ll INF = 1ll << 60;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class S, class T>
std::ostream& operator<<(std::ostream& out, const std::pair<S, T>& a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}
template <class T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {
  std::cout << '[';
  for (ll i = (0); i < (a.size()); ++i) {
    std::cout << a[i];
    if (i != a.size() - 1) std::cout << ", ";
  }
  std::cout << ']';
  return out;
}
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll N;
ll P[250010];
ll h[250010];
bool used[250010];
ll bfs(ll i, ll j) {
  ll ret = h[i * N + j];
  ll nh = h[i * N + j] + used[i * N + j];
  for (ll d = (0); d < (4); ++d) {
    ll ni = i + dy[d], nj = j + dx[d];
    if (nh < h[ni * N + nj]) {
      h[ni * N + nj] = nh;
      bfs(ni, nj);
    }
  }
  return ret;
}
int main() {
  std::cin >> N;
  for (ll i = (0); i < (N * N); ++i) std::cin >> P[i], --P[i];
  for (ll i = (0); i < (N * N); ++i) {
    ll r = i / N, c = i % N;
    h[i] = std::min({r, N - r - 1, c, N - c - 1});
    used[i] = true;
  }
  ll ans = 0;
  for (ll i = (0); i < (N * N); ++i) {
    ll r = P[i] / N;
    ll c = P[i] % N;
    used[r * N + c] = false;
    ans += bfs(r, c);
  }
  std::cout << ans << std::endl;
  return 0;
}
