#include<iostream>
#include<algorithm>
#define INF (1<<29)
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;

int main(){
	int a,b;
	int v[4];
	while(cin>>a>>b,a||b){
		int ans=INF;
		for(int i=1;i*i<=a;i++){
			if(a%i)continue;
			for(int j=1;j*j<=b;j++){
				if(b%j)continue;
				v[0]=i;
				v[1]=a/i;
				v[2]=j;
				v[3]=b/j;
				sort(v,v+4);
				int s=0;
				rep(i,3){
					s += (v[i]-v[i+1])*(v[i]-v[i+1]);
				}
				ans = min(ans,s);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}