In 1936, a dictator Hiedler who aimed at world domination had a deep obsession with the Lost Ark. A person with this ark would gain mystic power according to legend. To break the ambition of the dictator, ACM (the Alliance of Crusaders against Mazis) entrusted a secret task to an archeologist Indiana Johns. Indiana stepped into a vast and harsh desert to find the ark.

Indiana finally found an underground treasure house at a ruin in the desert. The treasure house seems storing the ark. However, the door to the treasure house has a special lock, and it is not easy to open the door.

To open the door, he should solve a problem raised by two positive integers a and b inscribed on the door. The problem requires him to find the minimum sum of squares of differences for all pairs of two integers adjacent in a sorted sequence that contains four positive integers a1, a2 , b1 and b2 such that a = a1a2 and b = b1b2. Note that these four integers does not need to be different. For instance, suppose 33 and 40 are inscribed on the door, he would have a sequence 3, 5, 8, 11 as 33 and 40 can be decomposed into 3 × 11 and 5 × 8 respectively. This sequence gives the sum (5 - 3)2 + (8 - 5)2 + (11 - 8)2 = 22, which is the smallest sum among all possible sorted sequences. This example is included as the first data set in the sample input.

Once Indiana fails to give the correct solution, he will suffer a calamity by a curse. On the other hand, he needs to solve the problem as soon as possible, since many pawns under Hiedler are also searching for the ark, and they might find the same treasure house. He will be immediately killed if this situation happens. So he decided to solve the problem by your computer program.

Your task is to write a program to solve the problem presented above.



Input

The input consists of a series of data sets. Each data set is given by a line that contains two positive integers not greater than 10,000.

The end of the input is represented by a pair of zeros, which should not be processed.

Output

For each data set, print a single integer that represents the minimum square sum in a line. No extra space or text should appear.

Example

Input

33 40
57 144
0 0


Output

22
94
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };

int main() {
	int x, y;
	while (cin >> x >> y, x){
		vector<int> xv;
		vector<int> yv;
		for (int i = 1; i <= sqrt(x); i++){
			if (x%i == 0)xv.push_back(i);
		}
		for (int i = 1; i <= sqrt(y); i++){
			if (y%i == 0)yv.push_back(i);
		}
		vector<int> calc(4);
		int ans = 1000000007;
		REP(i, xv.size()){
			REP(j, yv.size()){
				calc[0] = xv[i];
				calc[1] = x / xv[i];
				calc[2] = yv[j];
				calc[3] = y / yv[j];
				SORT(calc);
				ans = min(ans, (calc[0] - calc[1])*(calc[0] - calc[1]) + (calc[2] - calc[1])*(calc[2] - calc[1]) + (calc[3] - calc[2])*(calc[3] - calc[2]));
			}
		}
		cout << ans << endl;
	}
}