import numpy as np N = int(input()) grid = [] for i in range(N): row = [int(c) for c in input().split(' ')] grid.append(row) grid = np.array(grid) j_s = np.tile(np.arange(N), (N,1)) i_s = np.tile(np.arange(N).reshape(N, 1), N) distances = [ np.where((i_s + j_s >= x) & (i_s - j_s <= x), j_s, abs(x - i_s)) for x in range(N) ] # 各社員に対して、最適な攻撃位置を求める attack_distance = np.zeros_like(grid) optimal_sum = 0 for k in range(N): ks_house = np.where(grid == k+1, 1, 0) ks_house_distance = [] for x in range(N): # 位置(x, 1)から攻撃したときのドローンの距離の総和 ks_house_distance.append(np.where(ks_house == 1, distances[x], 0).sum()) optimal_sum += np.min(ks_house_distance) print(optimal_sum)