#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define Rep(i,a,n)for(int i=(int)(a);i<(int)(n);i++)
#define rep(i,n)Rep(i,0,n)
typedef long long lld;
typedef long double Lf;
#define Ma(x,y) (x>y?x:y)
#define mi(x,y) (x<y?x:y)

int main() {
	int n, p[10][10] = { 0 }, x[100] = { 0 }, y[100] = { 0 }, X, Y, ans = 0;
	scanf("%d", &n);
	rep(i, n)rep(j, n) { 
		scanf("%d", &X);
		Y=(X-1)/n+1;
		y[i * n + j + 1] = Y;
		X=(X%n?X%n:n);
		x[i * n + j + 1] = X;
		p[X][Y] = i * n + j + 1;
	}
	//rep(i, n* n + 3)printf("%d番目に抜けるのは(%d,%d)\n", i, x[i], y[i]);
	Rep(i, 1, 1 + n * n) {
		int flag = 1, search = 1;
		//p[x[i]][y[i]] = 0;
		while (flag) {
			rep(j, search)if (!p[x[i] + j][y[i] + search - j])flag = 0;
			rep(j, search)if (!p[x[i] + search - j][y[i] - j])flag = 0;
			rep(j, search)if (!p[x[i] - j][y[i] - search + j])flag = 0;
			rep(j, search)if (!p[x[i] - search + j][y[i] + j])flag = 0;
			search++;
		}
		p[x[i]][y[i]] = 2 - search;
		//printf("%dで%d+\n", i, search - 2);
		ans += search - 2;
	}
	printf("%d", ans);
}