#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
template <class T> void chmin(T &a, const T &b) noexcept { if (b < a) a = b; }
template <class T> void chmax(T &a, const T &b) noexcept { if (a < b) a = b; }
void debug_out() { cout << "\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &... args) { cout << x << " "; debug_out(args...);}
#ifdef _DEBUG
  #define debug(...) debug_out(__VA_ARGS__)
#else
  #define debug(...) 
#endif

/*
  delta(V v, update(V t))
  index(V v) -> int
*/
template<class V, class Delta, class Index>
ll bfs01(int n, V s, Delta delta, Index index) {
  vector<ll> dist(n,numeric_limits<ll>::max());
  using VP = pair<V,ll>;
  deque<VP> que;
  dist[index(s)] = 0;
  que.push_back({s, dist[index(s)]});
  ll min_cost = numeric_limits<ll>::max();
  while(!que.empty()) {
    V v = que.front().first;
    ll d = que.front().second;
    que.pop_front();
    if (dist[index(v)] < d) continue;
    if (min_cost < d) continue;
    auto update = [&](V t, ll weight) {
      if (dist[index(t)] > dist[index(v)] + weight) {
        dist[index(t)] = dist[index(v)] + weight;
        if (index.on_edge(t)) {
          chmin(min_cost, dist[index(t)]); //辺に到達
        }
        if (weight == 1) que.push_back({t, dist[index(t)]});
        else que.push_front({t, dist[index(t)]});
      }
    };
    delta(v,update);
  }
  return min_cost;
}

template<class F>
struct grid_delta {
  int h,w;
  F caller;
  const int dx[8] = {-1,0,1,0,-1,1,-1,1};
  const int dy[8] = {0,-1,0,1,-1,-1,1,1};
  grid_delta(int h, int w, F f):h(h),w(w),caller(f){}
  template<class G>
  void operator()(P v, G update) {
    int y = v.first, x = v.second;
    for (int i=0; i<4; ++i) {
      int ny = y+dy[i], nx = x+dx[i];
      if (nx<0 || ny<0 || nx>=w || ny>=h) continue;
      caller(v, P(ny,nx), update);
    }
  }
};

template<class F>
grid_delta<F> make_grid_delta(int h, int w, F f) { return grid_delta<F>(h, w, f);}

struct grid_index {
  int h,w;
  grid_index(int h, int w):h(h),w(w){}
  int operator()(P v){ return v.first*w+v.second;}
  P inv(int ind){ return P((ind-(ind%w))/w ,ind%w);}
  bool on_edge(P v){ return v.first==h-1||v.first==0||v.second==w-1||v.second==0;}
};

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int n;
  cin >> n;
  vector<int> p(n*n);
  rep(i,n*n) {
    cin >> p[i];
    p[i]--;
  }
  vector<vector<int> > G(n, vector<int>(n));
  auto index = grid_index(n,n);
  rep(y,n) rep(x,n) {
    G[y][x] = index(P(y,x));
  }
  auto caller = [&](P v, P t, auto update) {
    if (G[t.first][t.second] == -1) update(t, 0);
    else update(t, 1);
  };
  auto delta = make_grid_delta(n,n,caller);

  ll ans = 0;
  rep(i,n*n) {
    P start = index.inv(p[i]);
    G[start.first][start.second] = -1;
    if (index.on_edge(start)) continue; //すでに辺にいる
    auto res = bfs01(n*n, start, delta, index);
    ans += res;
  }
  cout << ans << endl;
  return 0;
}