#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U>& l, const std::pair<T, U>& r) {
  return {l.first + r.first, l.second + r.second};
}
const long long ll mod = 1e18;
const long long ll MAX_NODES = 200001;
const long long ll MAX_LOG = 50;
const long long ll maxn = 100000 + 1;
long long int n;
long long int p[505 * 505], cost[505][505];
map<long long int, pair<long long int, long long int>> rm;
long long int dx[4] = {1, 0, -1, 0};
long long int dy[4] = {0, 1, 0, -1};
bool isvalid(long long int i, long long int j) {
  return (i >= 1 && i <= n && j >= 1 && j <= n);
}
long long int calc(long long int id) {
  long long int i, j;
  tie(i, j) = rm[id];
  deque<pair<long long int, long long int>> q;
  vector<vector<long long int>> dist(n + 2, vector<long long int>(n + 2, 1e6));
  for (long long int i = 1; i <= n; i++) {
    q.push_back({0, i});
    q.push_back({i, 0});
    q.push_back({n + 1, i});
    q.push_back({i, n + 1});
    dist[0][i] = 0;
    dist[i][0] = 0;
    dist[n + 1][i] = 0;
    dist[i][n + 1] = 0;
  }
  while (!q.empty()) {
    auto u = q.front();
    q.pop_front();
    for (long long int k = 0; k < 4; k++) {
      long long int ni = u.first + dx[k];
      long long int nj = u.second + dy[k];
      if (isvalid(ni, nj)) {
        if (dist[ni][nj] > dist[u.first][u.second] + cost[ni][nj]) {
          dist[ni][nj] = dist[u.first][u.second] + cost[ni][nj];
          if (cost[ni][nj] == 0) {
            q.push_front({ni, nj});
          } else {
            q.push_back({ni, nj});
          }
        }
      }
    }
  }
  return dist[i][j];
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (long long int i = 1; i <= n * n; i++) {
    cin >> p[i];
  }
  for (long long int i = 1; i <= n; i++) {
    for (long long int j = 1; j <= n; j++) {
      cost[i][j] = 1;
      rm[n * (i - 1) + j] = {i, j};
    }
  }
  long long int ans = 0;
  for (long long int i = 1; i <= n * n; i++) {
    long long int k, j;
    tie(k, j) = rm[p[i]];
    cost[k][j] = 0;
    ans += calc(p[i]);
  }
  cout << ans << endl;
}
