Under construction

Still being built. Things may change

Ruins

Time limit8sMemory limit512 MB

Summary
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.
Level

Medium7 of 10

Topics
Math, Number theory, Brute force, Sorting
Solved
No attempts yet

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 x1≤x2≤x3≤x4x_1 \le x_2 \le x_3 \le x_4 and compute (x2−x1)2+(x3−x2)2+(x4−x3)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 (5−3)2+(8−5)2+(11−8)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.

Examples1

  1. Example 1

    Input
    33 40
    57 144
    0 0
    
    Expected output
    22
    94