package chelper; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import io.InputReader; import io.OutputWriter; import misc.GCJSolution; public class TaskC extends GCJSolution { public void solve(int testNumber, InputReader in, OutputWriter out) { wrapSolve(testNumber, in, out); } @Override public void solve(int testNumber) { int n = in.nextInt(); int[][] a = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { a[i][j] = in.nextInt(); } } Map> mxs1 = new HashMap<>(); Map> mys1 = new HashMap<>(); Map> mxs2 = new HashMap<>(); Map> mys2 = new HashMap<>(); Map counts1 = new HashMap<>(); Map counts2 = new HashMap<>(); Map total = new HashMap<>(); int changes = 0; for (int i = -n; i <= n; i++) { mxs1.put(i, new HashSet<>()); mys1.put(i, new HashSet<>()); counts1.put(i, 0); mxs2.put(i, new HashSet<>()); mys2.put(i, new HashSet<>()); counts2.put(i, 0); total.put(i, 0); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int f = a[i][j]; total.put(f, total.get(f) + 1); if (!mxs1.get(f).contains(i) && !mys1.get(f).contains(j)) { counts1.put(f, counts1.get(f) + 1); mxs1.get(f).add(i); mys1.get(f).add(j); } else { if (!mxs2.get(f).contains(i) && !mys2.get(f).contains(j)) { counts2.put(f, counts2.get(f) + 1); mxs2.get(f).add(i); mys2.get(f).add(j); } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int f = a[i][j]; if (!mxs1.get(f).contains(i) && !mys1.get(f).contains(j)) { counts1.put(f, counts1.get(f) + 1); mxs1.get(f).add(i); mys1.get(f).add(j); } else { if (!mxs2.get(f).contains(i) && !mys2.get(f).contains(j)) { counts2.put(f, counts2.get(f) + 1); mxs2.get(f).add(i); mys2.get(f).add(j); } } } } for (int f = -n; f <= n; f++) { int maxLeft = Math.max(counts1.get(f), counts2.get(f)); changes += total.get(f) - maxLeft; } out.println(changes); } }