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[][] a = new int[5][]; for(int i = 0;i < 5;i++){ a[i] = na(5); } long[][] bells = bells(5); List[][][] dp = new List[1<<5][52][2]; // 01,frontier,hidden for(int f = 0;f < 1<<5;f++){ int[] q = new int[5]; int p = 0; for(int i = 1;i < 5;i++){ if((f>>>i&1) != (f>>>i-1&1)){ p++; } q[i] = p; } int code = encBell(q, bells); dp[f][code][0] = new ArrayList(); long s = 0; for(int i = 0;i < 5;i++){ s += f<<~i<0 ? a[0][i] : -a[0][i]; } dp[f][code][0].add(s); } for(int i = 1;i < 5;i++){ for(int j = 0;j < 5;j++){ List[][][] ndp = new List[1<<5][52][2]; // 01,frontier,hidden for(int f = 0;f < 1<<5;f++){ for(int t = 0;t < 52;t++){ int[] e = decBell(t, 5, bells); for(int h = 0;h < 2;h++){ if(dp[f][t][h] == null)continue; for(int nv = 0;nv < 2;nv++){ int nf = (f<<1|nv)&31; int[] ne = new int[5]; for(int k = 0;k < 4;k++)ne[k+1] = e[k]; int nh = h; if((f>>>4&1) != nv){ boolean ok = false; for(int k = 0;k < 4;k++){ if(e[k] == e[4])ok = true; } if(!ok)nh++; } if(nh < 2){ ne[0] = 10; if((f>>>4&1) == nv){ for(int k = 1;k < 5;k++){ if(ne[k] == e[4]){ ne[k] = 10; } } } if(j > 0 && (f>>>0&1) == nv){ for(int k = 1;k < 5;k++){ if(ne[k] == e[0]){ ne[k] = 10; } } } ne = relocate(ne); int code = encBell(ne, bells); if(ndp[nf][code][nh] == null){ ndp[nf][code][nh] = new ArrayList(); } 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<<5;i++){ for(int j = 0;j < 52;j++){ for(int h = 0;h < 2;h++){ if(dp[i][j][h] == null)continue; int[] e = decBell(j, 5, bells); int max = 0; for(int v : e)max = Math.max(max, v); if(max+1 + h <= 2){ for(long v : (ArrayList)dp[i][j][h]){ min = Math.min(min, Math.abs(v)); } } } } } out.println(min); } public static int[] newcell(int[] a, int m, boolean hasLeft) { int n = a.length; int[] xa = new int[n]; System.arraycopy(a, 1, xa, 0, n-1); if(a[n-m] != -1){ xa[n-1] = a[n-m]; if(hasLeft && a[n-1] != -1){ for(int i = 0;i < n-1;i++){ if(xa[i] == a[n-1])xa[i] = xa[n-1]; } } }else if(hasLeft && a[n-1] != -1){ xa[n-1] = a[n-1]; }else{ xa[n-1] = n+1; } 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)); } }