Ruins

For each pair a, b up to 10000, choose factorizations a=a1*a2 and b=b1*b2 to order four numbers so the sum of squared consecutive gaps is minimal.

Medium7MathNumber theoryBrute forceSortingNo attempts yetTime limit8sMemory limit512 MB

Problem

An explorer finds the door of an underground treasure house in desert ruins. Two positive integers aa and bb are carved on the door, and the lock opens only after the problem posed by those two numbers is solved.

Choose positive integers a1,a2,b1,b2a_1, a_2, b_1, b_2 with a=a1×a2a = a_1 \times a_2 and b=b1×b2b = b_1 \times b_2. The four numbers may be equal to each other. Sort the four numbers in non-decreasing order as x1x2x3x4x_1 \le x_2 \le x_3 \le x_4 and compute (x2x1)2+(x3x2)2+(x4x3)2(x_2 - x_1)^2 + (x_3 - x_2)^2 + (x_4 - x_3)^2. Find the smallest value this expression takes over all choices.

For example, with a=33a = 33 and b=40b = 40, the factorizations 33=3×1133 = 3 \times 11 and 40=5×840 = 5 \times 8 give the sequence 3,5,8,113, 5, 8, 11. Its value is (53)2+(85)2+(118)2=22(5 - 3)^2 + (8 - 5)^2 + (11 - 8)^2 = 22, and no choice gives a smaller value.

Input

The input consists of several data sets. Each line holds two positive integers not greater than 10,000. A line with two zeros marks the end of the input and is not processed.

Output

For each data set, print the minimum value as a single integer on its own line. Print no extra spaces or characters.