#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  long long int arr[n * n], jerzy[n][n];
  for (int i = 1; i <= n * n; ++i) {
    cin >> arr[i - 1];
    if (i % n == 0)
      jerzy[i / n - 1][n - 1] = i;
    else
      jerzy[i / n][i % n - 1] = i;
  }
  int res(0);
  for (int i = 0; i < n * n; ++i) {
    if (arr[i] % n == 1 || arr[i] % n == 0 || arr[i] <= n ||
        arr[i] >= (n * n - n - 1)) {
      jerzy[arr[i] / n][arr[i] % n] = 0;
      continue;
    } else {
      int ans(0), ans1(0), ans2(0), ans3(0);
      for (int l = arr[i] / n - 1; l >= 0; --l) {
        if (jerzy[l][arr[i] % n - 1]) ++ans1;
      }
      for (int l = arr[i] / n + 1; l < n; ++l) {
        if (jerzy[l][arr[i] % n - 1]) ++ans1;
      }
      for (int k = 0; k < arr[i] % n; ++k) {
        if (jerzy[arr[i] / n][k]) ++ans2;
      }
      for (int l = n - 1; l >= arr[i] % n; --l) {
        if (jerzy[arr[i] / n][l]) ++ans3;
      }
      res += min({ans, ans1, ans2, ans3});
    }
    cout << res << "\n";
  }
}
