package contest190809; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.List; public class E { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = 5, m = 5; int[][] a = new int[n][]; for(int i = 0;i < n;i++){ a[i] = na(m); } long[][] bells = bells(m); int B = (int)bells[m][0]; List[][][] dp = new List[1<>>i&1) != (f>>>i-1&1)){ p++; } q[i] = p; } int code = encBell(q, bells); long s = 0; for(int i = 0;i < m;i++){ s += f<<~i<0 ? a[0][i] : -a[0][i]; } if(s >= 0){ dp[f][code][0] = new ArrayList(); dp[f][code][0].add(s); } } for(int i = 1;i < n;i++){ for(int j = 0;j < m;j++){ List[][][] ndp = new List[1<>>1|nv< 0 && (f>>>m-1&1) == nv); int nh = h + newHidden(e, (f&1) == nv); if(nh < 2){ int code = encBell(ne, bells); if(ndp[nf][code][nh] == null){ ndp[nf][code][nh] = new ArrayList(); } // tr(i, j, f, e, h, nf, ne, nh, dp[f][t][h]); for(long x : (ArrayList)dp[f][t][h]){ ndp[nf][code][nh].add(x + (nv == 1 ? a[i][j] : -a[i][j])); } } } } } } dp = ndp; } } long min = Long.MAX_VALUE; for(int i = 0;i < 1<)dp[i][j][h]){ min = Math.min(min, Math.abs(v)); } } } } } out.println(min); } public static int newHidden(int[] a, boolean connectToUpper) { if(connectToUpper)return 0; if(a[0] != -1){ for(int i = 1;i < a.length;i++){ if(a[i] == 0)return 0; } return 1; }else{ return 0; } } public static int[] newcell(int[] a, int m, boolean connectToUpper, boolean connectToLeft) { int n = a.length; int[] xa = new int[n]; System.arraycopy(a, 1, xa, 0, n-1); xa[n-1] = n; if(connectToUpper){ for(int i = 0;i < n-1;i++){ if(xa[i] == a[n-m])xa[i] = n; } } if(connectToLeft){ for(int i = 0;i < n-1;i++){ if(xa[i] == a[n-1])xa[i] = n; } } return relocate(xa); } public static int[] relocate(int[] a) { int[] map = new int[12]; Arrays.fill(map, -1); int p = 0; for(int i = 0;i < a.length;i++){ if(a[i] == -1)continue; if(map[a[i]] == -1){ map[a[i]] = p++; } a[i] = map[a[i]]; } return a; } public static long[][] bells(int n) { long[][] bells = new long[n+1][]; for(int i = 0;i <= n;i++)bells[i] = new long[n-i+1]; Arrays.fill(bells[0], 1); for(int i = 1;i <= n;i++){ for(int j = 0;j <= n-i;j++){ bells[i][j] = bells[i-1][j] * j + bells[i-1][j+1]; } } return bells; } // クラスタ分布から最小完全ハッシュ public static int encBell(int[] a, long[][] bells) { int max = 0; int ret = 0; for(int i = 0;i < a.length;i++){ ret += bells[a.length-i-1][max+1] * a[i]; if(a[i] > max)max = a[i]; } return ret; } public static int[] decBell(long K, int m, long[][] bells) { if(K >= bells[m][0])return null; int[] a = new int[m]; int max = 0; for(int i = 0;i < m;i++){ a[i] = (int)(Math.min(max+1, K/bells[m-i-1][max+1])); K -= a[i] * bells[m-i-1][max+1]; if(a[i] > max)max = a[i]; } return a; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new E().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }