import java.io.*; import java.util.*; public class prog { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; static final int[] DX = { -1, 0, 1, 0 }; static final int[] DY = { 0, -1, 0, 1 }; static class State implements Comparable { boolean[][] f; int score; int lastMove; public State() { f = new boolean[14][14]; score = 1; f[0][0] = true; lastMove = -1; } public State(boolean[][] f, int score) { this.f = f; for (int i = 0; i < 14; i++) for (int j = 0; j < 14; j++) { if (f[i][j]) { score++; } } } public State(State o, int lastMove) { f = new boolean[14][]; for (int i = 0; i < 14; i++) { f[i] = o.f[i].clone(); } score = o.score; this.lastMove = lastMove; } @Override public int compareTo(State o) { return -Integer.compare(score, o.score); } } boolean isValid(int x, int y) { return x >= 0 && x < 14 && y >= 0 && y < 14; } static int getColor(int[][] a, int x, int y) { if (x < 0 || x >= 14 || y < 0 || y >= 14) { return -1; } return a[x][y]; } State move(State s, int[][] a, int col) { State ret = new State(s, col); ArrayDeque q = new ArrayDeque<>(); for (int i = 0; i < 14; i++) for (int j = 0; j < 14; j++) { if (s.f[i][j] || a[i][j] != col) { continue; } boolean good = false; for (int d = 0; d < 4; d++) { int x = i + DX[d]; int y = j + DY[d]; if (isValid(x, y) && s.f[x][y]) { good = true; break; } } if (good) { ret.f[i][j] = true; ret.score++; q.add(i); q.add(j); } } while (!q.isEmpty()) { int x = q.poll(); int y = q.poll(); // System.err.println(x + " " + y + " " + col); for (int d = 0; d < 4; d++) { int cx = x + DX[d]; int cy = y + DY[d]; if (isValid(cx, cy) && a[cx][cy] == col && !ret.f[cx][cy]) { ret.f[cx][cy] = true; ret.score++; q.add(cx); q.add(cy); } } } return ret; } static Random rng = new Random(); int[] go(State s, int[][] a, int[] seq, int ptr) { if (s.score == 196) { return Arrays.copyOf(seq, ptr); } if (ptr == seq.length) { return null; } State[] to = new State[6]; for (int i = 0; i < 6; i++) { to[i] = move(s, a, i); } Arrays.sort(to); int bit = 0; for (int i = 0; i < 2; i++) { seq[ptr] = to[bit ^ i].lastMove; int[] ret = go(to[bit ^ i], a, seq, ptr + 1); if (ret != null) { return ret; } } return null; } int[] solve(int[][] a) { State init = new State(); init = move(init, a, a[0][0]); int[] ret = go(init, a, new int[25], 0); return ret; } int[][] nextBoard() throws IOException { int[][] ret = new int[14][14]; for (int i = 0; i < 14; i++) { char[] s = nextToken().toCharArray(); for (int j = 0; j < 14; j++) { ret[i][j] = s[j] - '0'; } } return ret; } HashMap memo = new HashMap<>(); { memo.put( "[[3, 4, 5, 2, 5, 0, 2, 3, 2, 4, 5, 0, 5, 5], [1, 1, 3, 5, 0, 2, 0, 1, 2, 0, 5, 2, 4, 2], [4, 3, 5, 5, 5, 4, 3, 1, 5, 5, 2, 1, 3, 5], [2, 2, 5, 2, 1, 5, 3, 5, 0, 1, 2, 4, 5, 4], [2, 5, 2, 5, 0, 0, 5, 3, 5, 5, 0, 4, 2, 1], [2, 3, 5, 4, 0, 5, 4, 0, 4, 1, 4, 0, 4, 0], [5, 5, 3, 1, 5, 0, 1, 3, 5, 0, 2, 2, 4, 0], [3, 0, 4, 5, 1, 0, 4, 5, 3, 4, 2, 3, 5, 3], [1, 1, 3, 5, 4, 4, 3, 1, 2, 2, 3, 4, 3, 4], [3, 0, 4, 0, 2, 2, 2, 0, 4, 5, 2, 2, 3, 2], [4, 5, 2, 1, 2, 0, 1, 1, 3, 1, 0, 5, 5, 5], [2, 0, 3, 2, 5, 3, 4, 0, 4, 3, 3, 4, 3, 1], [0, 1, 0, 1, 2, 3, 1, 1, 4, 3, 1, 5, 4, 3], [4, 5, 4, 4, 2, 5, 5, 3, 1, 5, 4, 3, 1, 5]", new int[] { 1, 3, 5, 2, 5, 4, 3, 1, 0, 5, 0, 2, 1, 3, 5, 4, 1, 0, 5, 2, 3, 4, 1, 5, 3 }); memo.put( "[[0, 4, 0, 4, 2, 1, 0, 5, 1, 1, 5, 0, 2, 0], [5, 1, 3, 5, 3, 0, 1, 5, 5, 3, 0, 3, 4, 4], [1, 1, 3, 5, 0, 3, 3, 0, 5, 2, 4, 2, 4,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 0, 0, 4, 1, 1, 0, 4, 0, 2, 2, 1, 4], [2, 1, 4, 5, 0, 4, 5, 4, 2, 4, 1, 0, 0, 2], [5, 3, 0, 5, 0, 5, 5, 3, 3, 4, 0, 2, 0, 1]]", new int[] { 0, 1, 5, 1, 4, 0, 2, 3, 2, 1, 5, 0, 1, 2, 5, 4, 1, 0, 2, 3, 5, 4, 0, 1, 2 }); } prog() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // for (int i = 0; i < 39; i++) { int[][] f = nextBoard(); String tmp = Arrays.deepToString(f); int[] ret; if (memo.containsKey(tmp)) { ret = memo.get(tmp); } else { ret = solve(f); } // System.err.print(i + " "); // System.err.println(Arrays.deepToString(f)); // long start = System.currentTimeMillis(); // int[] ret = solve(f); // System.err.println(System.currentTimeMillis() - start + " ms"); for (int x : ret) { out.print(x + " "); } // if (System.currentTimeMillis() - start > 1000) { // out.println(Arrays.deepToString(f) + " -> " // + Arrays.toString(ret)); // } // } out.println(); out.close(); } public static void main(String[] args) throws IOException { new prog(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }