// Arup Guha
// 9/20/2013
// Solution to 2010 ACPC Problem B: Sum the Square

import java.util.*;

public class b {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		int a = stdin.nextInt();
		int b = stdin.nextInt();

		// Process all cases.
		while (a != 0 ||  b != 0) {

			// Get both sequences until they hit 89 or 1, the second time.
			ArrayList<Integer> listA = getList(a);
			ArrayList<Integer> listB = getList(b);


			System.out.println(a+" "+b+" "+getMin(listA, listB));
			// Get next case.
			a = stdin.nextInt();
			b = stdin.nextInt();
		}
	}

	public static int getMin(ArrayList<Integer> listA, ArrayList<Integer> listB) {

		int ans = listA.size() + listB.size() + 1;

		// This is awful also...but the lists are small, I promise =)
		for (int i=0; i<listA.size(); i++) {
			for (int j=0; j<listB.size(); j++) {

				// Maybe this will help? But it's unnecessary.
				if (i+j+2 > ans) break;

				if (listA.get(i).equals(listB.get(j)) && i+j+2 < ans)
					ans = i+j+2;
			}
		}

		// Return what they want.
		if (ans > listA.size() + listB.size()) return 0;
		return ans;
	}

	// Peel off each digit, square and add...
	public static int getSquares(int a) {
		int sum = 0;
		while (a > 0) {
			sum += (a%10)*(a%10);
			a /= 10;
		}
		return sum;
	}

	public static ArrayList<Integer> getList(int a) {

		ArrayList<Integer> list = new ArrayList<Integer>();

		// Just create it, it can't be too long, do you see why?
		while (a != 89 && a != 1) {
			list.add(a);
			a = getSquares(a);
		}
		list.add(a);

		// This is hacky, just tack on the last cycle.
		if (a == 89) {
			int[] extra = {145, 42, 20, 4, 16, 37, 58};
			for (int i=0; i<extra.length; i++)
				list.add(extra[i]);
		}

		return list;
	}
}