#include <bits/stdc++.h>
using namespace std;
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;
}
const long long INF = 1LL << 60;
vector<pair<long long int, long long int> > p;
bool pairCompare(const pair<double, long long int>& firstElof,
                 const pair<double, long long int>& secondElof) {
  return firstElof.first > secondElof.first;
}
bool pairCompareSecond(const pair<double, long long int>& firstElof,
                       const pair<double, long long int>& secondElof) {
  return firstElof.second < secondElof.second;
}
bool x[100100];
long long int num[100100];
long long int fibl[100100] = {0};
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
long long int fib(long long int a) {
  if (fibl[a] != 0) return fibl[a];
  if (a == 0) {
    return 0;
  } else if (a == 1) {
    return 1;
  }
  return fibl[a] = fib(a - 1) + fib(a - 2);
}
long long int eratosthenes(long long int n) {
  int p = 0;
  for (long long int i = 0; i <= n; ++i) x[i] = true;
  x[0] = x[1] = false;
  for (int i = 2; i <= n; ++i) {
    if (x[i]) {
      p++;
      for (int j = 2 * i; j <= n; j += i) x[j] = false;
    }
    num[i] = p;
  }
  return p;
}
long long int gcd(long long int a, long long int b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}
long long int keta(long long int N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}
void base_num(long long int data, long long int base) {
  if (data == 0) return;
  long long int next = abs(data) % abs(base);
  if (data < 0) next = (abs(base) - next) % abs(base);
  base_num((data - next) / base, base);
  cout << next;
}
long long int n, ans = 0;
bool s[510][510];
long long int visited[510][510];
void dfs(long long int x, long long int y) {
  if (x == 0 or y == 0 or x == n - 1 or y == n - 1) {
    ans = min(ans, visited[x][y]);
  } else {
    for (long long int i = 0; i < 4; i++) {
      long long int a = x + dx[i], b = y + dy[i];
      if (a >= 0 and a < n and b >= 0 and b < n and
          visited[a][b] > visited[x][y]) {
        if (s[a][b]) {
          if (visited[a][b] == visited[x][y] + 1) continue;
          visited[a][b] = visited[x][y] + 1;
          if (visited[a][b] < ans) dfs(a, b);
        } else {
          visited[a][b] = visited[x][y];
          if (visited[a][b] < ans) dfs(a, b);
        }
      }
    }
  }
}
int main() {
  cin >> n;
  long long int p[n * n], c = 0;
  for (long long int i = 0; i < n * n; i++) {
    cin >> p[i];
  }
  for (long long int i = 0; i < n; i++) fill(s[i], s[i] + n, true);
  for (long long int i = 0; i < n * n; i++) {
    ans = INF;
    long long int x = (p[i] - 1) / n, y = (p[i] - 1) % n;
    for (long long int i = 0; i < n; i++) fill(visited[i], visited[i] + n, INF);
    s[x][y] = false;
    visited[x][y] = 0;
    dfs(x, y);
    c += ans;
  }
  cout << c << endl;
  return 0;
}
