#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<map>
#include<cassert>
#include<ctime>
#include<set>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define all(n) n.begin(),n.end()
using namespace std;
int a,b;

int main(){
 
  while(true){
    cin >> a >> b;
    if(a+b == 0)break;
    int ans = (1<<27);
    assert(a>=0 && b>=0);
    for(int i=1;i<=a/2+1;i++){
      if(a%i != 0)continue;
      vector<int> vec;
      //cout << "a = " << i << "," << a/i << " ";
      vec.push_back(i);vec.push_back((int)(a/i));
      for(int j=1;j<=b/2+1;j++){
	if(b%j != 0)continue;
	//cout << "b = " << j << "," << b/j << endl;
	vec.push_back(j); vec.push_back(b/j);
	sort(all(vec));
	//for(int k=0;k<4;k++){
	//cout << vec[k] << endl;
	//}
	//cout << "---" << endl;
	int sum = 0;
	sum = (vec[1]-vec[0])*(vec[1]-vec[0]) + (vec[2]-vec[1])*(vec[2]-vec[1]) + (vec[3]-vec[2])*(vec[3]-vec[2]);
	//cout << "sum -  " << sum << endl;
	ans = min(ans,sum);
	vec.clear();
	vec.push_back(i); vec.push_back(a/i);
      }
    }

    cout << ans << endl;
  }
  
  return 0;
}