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

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};
int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int aa[10000];

#define DBL(x) ((x)*(x))

int main(){
  int a,b;
  int tmp[4];

  while(a = getInt(), b = getInt(), a + b){
    int ca = 0;
    int ans = INT_MAX;
    for(int i=1; i*i <= a; i++)
      if(a % i == 0) aa[ca++] = i;
    for(int i=1; i*i <= b; i++){
      if(b % i == 0){
        REP(j,ca){
          tmp[0] = i;
          tmp[1] = b / i;
          tmp[2] = aa[j];
          tmp[3] = a / aa[j];
          //printf("%d %d %d %d\n",tmp[0],tmp[1],tmp[2],tmp[3]);
          sort(tmp, tmp+4);
          ans = min(ans, DBL(tmp[3]-tmp[2]) + DBL(tmp[2]-tmp[1]) + DBL(tmp[1]-tmp[0]));
        }
      }
    }
    print(ans);
  }
  return 0;
}