import java.io.*; import java.util.ArrayList; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); ArrayList arr[][] = new ArrayList[n][n]; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ arr[i][j] = new ArrayList(); } } for(int i = 0; i < n; i++){ String[] line = br.readLine().split(" "); for(int j = 0; j < n; j++){ arr[Integer.parseInt(line[j])-1][i].add(j); } } int ans = 0; for(int i = 0; i < n; i++){ int sum[] = new int[n]; for(int j = 0; j < n; j++){ for(int k : arr[i][j]){ sum[j] += k; } } int down[] = new int[n], tmp[] = new int[n]; int val = 0, acc = 0; for(int j = 0; j < n; j++){ for(int k : arr[i][j]){ if(j+k+1 < n) tmp[j+k+1]++; } val += sum[j]; acc += tmp[j]; val += acc; down[j] = val; } int up[] = new int[n]; Arrays.fill(tmp, 0); val = 0; acc = 0; for(int j = n-1; j >= 0; j--){ for(int k : arr[i][j]){ if(j-k-1 >= 0) tmp[j-k-1]++; } val += sum[j]; acc += tmp[j]; val += acc; up[j] = val; } int score = 1<<30; for(int j = 0; j < n; j++){ score = Math.min(score, down[j]+up[j]-sum[j]); } ans += score; } System.out.println(ans); } }