#include <iostream>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

int a, b;

int main() {
  while(1) {
    cin >> a >> b;
    if(a == 0 && b == 0) break;
    vector< multiset<int> > V;
    for(int i = 1; i <= a; i++) {
      if(a % i != 0) continue;
      for(int j = 1; j <= b; j++) {
	if(b % j != 0) continue;
	multiset<int> s;
	s.insert(i);
	s.insert(a/i);
	s.insert(j);
	s.insert(b/j);
	V.push_back(s);
      }
    }
    int min = -1;
    for(vector< multiset<int> >::iterator it = V.begin();
	it != V.end(); it++) {
      int sum = 0;
      int tmp[4];
      int index = 0;
      for(multiset<int>::iterator itms = it->begin();
	  itms != it->end(); itms++) {
	tmp[index++] = *itms;
      }
      for(int i = 0; i+1 < 4; i++) {
	sum += (int)pow(double(tmp[i] - tmp[i+1]), 2.0);
      }
      if(min == -1 || sum < min) {
	min = sum;
      }
    }
    cout << min << endl;
  }
}