#include <bits/stdc++.h>
using namespace std;
struct ca {
  char a;
  int si, ei;
};
int binarySearch(vector<int> arr, int l, int r, int x) {
  if (r >= l) {
    int mid = l + (r - l) / 2;
    if (arr[mid] == x) return mid;
    if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
    return binarySearch(arr, mid + 1, r, x);
  }
  return -1;
}
int prime[1000001] = {};
void pr() {
  prime[1] = 0;
  for (int i = 2; i * i < 1000001; i++)
    if (prime[i] == 0)
      for (int j = i * i; j < 1000001; j += i) prime[j] = 1;
}
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long modInverse(long long n, long long p) { return power(n, p - 2, p); }
long long fac[1000001] = {};
void facto(long long p) {
  fac[0] = 1;
  for (long long i = 1; i <= 1000000; i++) fac[i] = fac[i - 1] * i % p;
}
long long nCrModPFermat(long long n, long long r, long long p) {
  if (r == 0) return 1;
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
void fun(long long n, long long a, long long b, long long c, long long d,
         long long sum, long long cost, long long& ans) {
  if (sum >= n) {
    long long x = sum - n;
    cost += (x * d);
    if (cost < ans) ans = cost;
    return;
  }
  fun(n, a, b, c, d, 2 * sum, cost + a, ans);
  fun(n, a, b, c, d, 3 * sum, cost + b, ans);
  fun(n, a, b, c, d, 5 * sum, cost + c, ans);
}
int main() {
  int t;
  t = 1;
  int id = 1;
  while (t--) {
    int n;
    cin >> n;
    int arr[501][501] = {};
    vector<int> mr;
    for (int i = 0; i < n * n; i++) {
      int a;
      cin >> a;
      mr.push_back(a - 1);
    }
    int ans = 0;
    for (int i = 0; i < n * n; i++) {
      int r = mr[i] / n;
      int c = mr[i] % n;
      int minm = n;
      int curr = 0;
      for (int k = 0; k < c; k++)
        if (arr[r][k] == 0) curr++;
      minm = min(curr, minm);
      if (minm) {
        curr = 0;
        for (int k = c + 1; k < n; k++)
          if (arr[r][k] == 0) curr++;
        minm = min(curr, minm);
      }
      if (minm) {
        curr = 0;
        for (int k = r + 1; k < n; k++)
          if (arr[k][c] == 0) curr++;
        minm = min(curr, minm);
      }
      if (minm) {
        curr = 0;
        for (int k = 0; k < r; k++)
          if (arr[k][c] == 0) curr++;
        minm = min(curr, minm);
      }
      ans += minm;
      arr[r][c] = 1;
    }
    cout << ans;
  }
  return 0;
}
