#include <bits/stdc++.h>
using namespace std;
template <typename First>
void debug_print_in(First first) {
  cerr << first << endl;
  return;
}
template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}
void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest) {
  cin >> first;
  IN(rest...);
  return;
}
template <typename First>
void OUT(First first) {
  cout << first << endl;
  return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}
template <class t, class u>
void chmax(t& a, u b) {
  if (a < b) a = b;
};
template <class t, class u>
void chmin(t& a, u b) {
  if (a > b) a = b;
};
int popcount(int t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
template <typename T>
void vec_print(vector<T> VEC) {
  for (long long i = (long long)0; i < (long long)VEC.size(); ++i) {
    cerr << VEC[i] << " ";
  }
  cerr << endl;
};
template <typename T>
void mat_print(vector<vector<T>> MAT) {
  for (long long i = (long long)0; i < (long long)MAT.size(); ++i) {
    for (long long j = (long long)0; j < (long long)MAT[i].size(); ++j) {
      cerr << MAT[i][j] << " ";
    }
    cerr << endl;
  }
};
constexpr int INF = (1 << 30);
constexpr long long INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr long long MOD = (long long)((1E+9) + 7);
pair<pair<long long, long long>, pair<long long, long long>> mapa(
    long long x1, long long y1, long long x2, long long y2) {
  return make_pair(make_pair(x1, y1), make_pair(x2, y2));
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  IN(N);
  vector<long long> P(N * N);
  for (long long i = (long long)0; i < (long long)N * N; ++i) {
    IN(P[i]);
    P[i]--;
  }
  vector<vector<long long>> Cost(N, vector<long long>(N, 0));
  for (long long i = (long long)0; i < (long long)N; ++i) {
    for (long long j = (long long)0; j < (long long)N; ++j) {
      Cost[i][j] = min(min(i, N - 1 - i), min(j, N - 1 - j));
    }
  }
  map<pair<pair<long long, long long>, pair<long long, long long>>, long long>
      mp;
  for (long long i = (long long)0; i < (long long)N - 1; ++i) {
    for (long long j = (long long)0; j < (long long)N; ++j) {
      mp[mapa(i, j, i + 1, j)] = 1;
      mp[mapa(i + 1, j, i, j)] = 1;
      mp[mapa(j, i, j, i + 1)] = 1;
      mp[mapa(j, i + 1, j, i)] = 1;
    }
  }
  long long ans = 0;
  for (long long i = (long long)0; i < (long long)N * N; ++i) {
    long long Pi = P[i];
    long long x = Pi / N;
    long long y = Pi % N;
    ans += Cost[x][y];
    queue<long long> que;
    set<pair<long long, long long>> used;
    if (x > 0) {
      mp[mapa(x - 1, y, x, y)] = 0;
      que.push((x - 1) * N + y);
    }
    if (x < N - 1) {
      mp[mapa(x + 1, y, x, y)] = 0;
      que.push((x + 1) * N + y);
    }
    if (y > 0) {
      mp[mapa(x, y - 1, x, y)] = 0;
      que.push(x * N + y - 1);
    }
    if (y < N - 1) {
      mp[mapa(x, y + 1, x, y)] = 0;
      que.push(x * N + y + 1);
    }
    while (que.size()) {
      long long pos_tmp = que.front();
      que.pop();
      long long x_tmp = pos_tmp / N;
      long long y_tmp = pos_tmp % N;
      if (used.find(make_pair(x_tmp, y_tmp)) != used.end()) continue;
      used.emplace(make_pair(x_tmp, y_tmp));
      if (x_tmp == 0 || x_tmp == N - 1 || y_tmp == 0 || y_tmp == N - 1)
        continue;
      long long New_Cost = INFLL;
      {
        chmin(New_Cost, Cost[x_tmp - 1][y_tmp] +
                            mp[mapa(x_tmp, y_tmp, x_tmp - 1, y_tmp)]);
      }
      {
        chmin(New_Cost, Cost[x_tmp + 1][y_tmp] +
                            mp[mapa(x_tmp, y_tmp, x_tmp + 1, y_tmp)]);
      }
      {
        chmin(New_Cost, Cost[x_tmp][y_tmp - 1] +
                            mp[mapa(x_tmp, y_tmp, x_tmp, y_tmp - 1)]);
      }
      {
        chmin(New_Cost, Cost[x_tmp][y_tmp + 1] +
                            mp[mapa(x_tmp, y_tmp, x_tmp, y_tmp + 1)]);
      }
      if (New_Cost >= Cost[x_tmp][y_tmp]) continue;
      Cost[x_tmp][y_tmp] = New_Cost;
      que.push((x_tmp - 1) * N + y_tmp);
      que.push((x_tmp + 1) * N + y_tmp);
      que.push(x_tmp * N + y_tmp - 1);
      que.push(x_tmp * N + y_tmp + 1);
    }
  }
  OUT(ans);
  return 0;
}
