import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;


public class ANARC_2010_B_Alt
{
	BufferedReader in;
	Scanner inr;
	PrintWriter out;

	void initIO() throws IOException
	{
		inr = new Scanner(System.in);
		in = new BufferedReader(new InputStreamReader(
				System.in));
		out = new PrintWriter(new OutputStreamWriter(System.out));
	}
	
	int f(int n)
	{
		int res = 0;
		while (n > 0)
		{
			int d = n % 10; n /= 10;
			res += d * d;
		}
		return res;
	}
	
	Integer[] getCycle(int n)
	{
		Vector<Integer> r = new Vector<Integer>();
		Set<Integer> was = new HashSet<Integer>();
		r.add(n);
		was.add(n);
		while (true)
		{
			n = f(n);
			r.add(n);
			if (was.contains(n))
				break;
			was.add(n);
		}
		return r.toArray(new Integer[0]);
	}
	
	void solve() throws Exception
	{
		while (true)
		{
			int A = nextInt();
			int B = nextInt();
			if (A == 0 && B == 0)
				break;
			Integer[] vA = getCycle(A);
			int nA = vA.length;
			boolean fA = vA[nA-1] == 1;
			Integer[] vB = getCycle(B);
			int nB = vB.length;
			boolean fB = vB[nB-1] == 1;
			//for (int i = 0; i < vA.length; i++)
//				System.out.print(vA[i] + (i == vA.length - 1 ? "\n" : " "));
			//for (int i = 0; i < vB.length; i++)
//				System.out.print(vB[i] + (i == vB.length - 1 ? "\n" : " "));
			System.out.print("" + A + " " + B + " ");
			if (!(fA ^ fB))
			{
				int res = 100000;
				for (int iA = 0; iA < nA; iA++)
					for (int iB = 0; iB < nB; iB++)
						if (vA[iA].equals(vB[iB]))
						{
							res = Math.min(res, iA + iB + 2);
							//System.out.println(iA + " " + iB + " " + res);
						}
				System.out.println(res);
			}
			else
			{
				System.out.println("0");
			}
		}
	}

	void run() throws Exception
	{
		initIO();
		solve();
		out.flush();
	}

	int nextInt()
	{
		return inr.nextInt();
	}
	
	String nextLine()
	{
		return inr.nextLine();
	}
	
	String nextString()
	{
		return inr.next();
	}

	public static void main(String[] args) throws Exception
	{
		new ANARC_2010_B_Alt().run();
	}
}
