#include <bits/stdc++.h>
using namespace std;
int n;
int arr[2505];
long long dp[505][505];
vector<vector<int> > visit(505, vector<int>(505, 1));
long long cal1(long long x, long long y) {
  if (x <= 0 || x > n || y <= 0 || y > n) return 0;
  if (dp[x][y] != -1) return dp[x][y];
  long long ans = n * n;
  ans = min(ans, visit[x][y] + cal1(x + 1, y));
  ans = min(ans, visit[x][y] + cal1(x, y + 1));
  return dp[x][y] = ans;
}
long long cal2(long long x, long long y) {
  if (x <= 0 || x > n || y <= 0 || y > n) return 0;
  if (dp[x][y] != -1) return dp[x][y];
  long long ans = n * n;
  ans = min(ans, visit[x][y] + cal2(x + 1, y));
  ans = min(ans, visit[x][y] + cal2(x, y - 1));
  return dp[x][y] = ans;
}
long long cal3(long long x, long long y) {
  if (x <= 0 || x > n || y <= 0 || y > n) return 0;
  if (dp[x][y] != -1) return dp[x][y];
  long long ans = n * n;
  ans = min(ans, visit[x][y] + cal3(x - 1, y));
  ans = min(ans, visit[x][y] + cal3(x, y + 1));
  return dp[x][y] = ans;
}
long long cal4(long long x, long long y) {
  if (x <= 0 || x > n || y <= 0 || y > n) return 0;
  if (dp[x][y] != -1) return dp[x][y];
  long long ans = n * n;
  ans = min(ans, visit[x][y] + cal4(x - 1, y));
  ans = min(ans, visit[x][y] + cal4(x, y - 1));
  return dp[x][y] = ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  visit = vector<vector<int> >(n + 1, vector<int>(n + 1, 1));
  for (int i = 0; i < n * n; i++) cin >> arr[i];
  long long ans = 0;
  for (int i = 0; i < n * n; i++) {
    long long x = arr[i] / n;
    long long y = ((arr[i] % n) == 0 ? n : arr[i] % n);
    if (arr[i] % n) x++;
    visit[x][(arr[i] % n) == 0 ? n : arr[i] % n] = 0;
    memset(dp, -1, sizeof dp);
    long long tans1 = cal1(x, y);
    memset(dp, -1, sizeof dp);
    long long tans2 = cal2(x, y);
    memset(dp, -1, sizeof dp);
    long long tans3 = cal3(x, y);
    memset(dp, -1, sizeof dp);
    long long tans4 = cal4(x, y);
    ans += min(tans1, min(tans2, min(tans3, tans4)));
  }
  cout << ans << endl;
}
