/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.*; //import java.util.Arrays; import java.util.ArrayList; import java.util.HashSet; /** * * @author cteam025 */ public class collatz { public static final int MAX = 1000000*100+1;//(3*1000000+1+1)*80; /** * @param args the command line arguments */ public static void main(String[] args) throws IOException{ BufferedInputStream br = new BufferedInputStream(System.in); int first, second; //boolean arraA[] = new boolean[MAX]; //boolean arraB[] = new boolean[MAX]; HashSet arraA = new HashSet(10000); HashSet arraB = new HashSet(10000); int fi, se, oldfi, oldse; boolean isfi; ArrayList lfi = new ArrayList(10000), lse = new ArrayList(10000); //HashSet lfi = new HashSet(), lse = new HashSet(); while(((first = readInt(br)) != 0) && ((second = readInt(br)) != 0)) { //Arrays.fill(arraA, false); //Arrays.fill(arraB, false); arraA.clear(); arraB.clear(); lfi.clear();lse.clear(); fi = first; se = second; isfi = false; // while(!(arraA[fi] && arraB[fi]) || !(arraA[se] && arraB[se])) { // if(!arraB[se]) lse.add(new Integer(se)); // if(!arraA[fi]) lfi.add(new Integer(fi)); // arraA[fi] = true; arraB[se] = true; // fi = next(fi); se = next(se); // } int loa, lob, out; while(true) { //arraA[fi] = true; arraB[se] = true; arraA.add(new Integer(fi)); arraB.add(new Integer(se)); //if(arraA[fi] && arraB[fi]) {out = fi; break;} //if(arraA[se] && arraB[se]) {out = se; break;} if(arraA.contains(new Integer(fi)) && arraB.contains(new Integer(fi))) { isfi = true; out = fi; break;} if(arraA.contains(new Integer(se)) && arraB.contains(new Integer(se))) {out = fi; break;} lfi.add(new Integer(fi)); lse.add(new Integer(se)); oldfi = fi; oldse = se; fi = next(fi); se = next(se); } lfi.add(new Integer(fi)); lse.add(new Integer(se)); loa = lfi.size() - 1; lob = lse.size() - 1; if(isfi) { loa = lfi.size() - 1; lob = find(lse, fi); // out = fi; } else { loa = find(lfi, se); lob = lse.size() - 1; // out = se; } //print(lfi); //print(lse); //print(arraA); print(arraB); //System.out.println(lfi.get(loa)); //System.out.println(lse.get(lob)); System.out.println(first + " needs " + loa + " steps, " + second + " needs " + lob + " steps, they meet at " + out); } } public static int find(ArrayList l, int val) { int i = 0; for (Object object : l) { if(val == (((Integer) object).intValue())) return i; i++; } return -1; } public static void print(HashSet s) { for (Object object : s) { System.out.print(object.toString() + " "); } System.out.println(""); } public static void print(ArrayList l) { for (Object object : l) { System.out.print(object.toString() + " "); } System.out.println(""); } public static void print(boolean[] arr) { int i = 0; for (boolean val: arr) { if(val) System.out.print(i+" "); i++; } System.out.println(""); } public static int next(int i) { if(i % 2 == 0) return i/2; return 3*i + 1; } public static int readInt(InputStream in) throws IOException { int i = 0, j; while((j = in.read()) != '\n') { if(j == '\r') continue; if(j == ' ') break; i = (((i << 2) + i) << 1) + (j - 48); } //System.out.println(i); return i; } }