#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main(){
  int a, b;
  while(cin>>a>>b && a){
    ll ans = 1e12;
    for(int i = 1; i * i <= a; i++)if(a % i == 0){
      for(int j = 1; j * j <= b; j++) if(b % j == 0){
        ll sum = 0;
        ll v[4] = {i, j, a / i, b / j};
        sort(v, v + 4);
        REP(k, 3) sum += (v[k + 1] - v[k]) * (v[k + 1] - v[k]);
        ans = min(ans, sum);
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}