/*
  AOJ 2030
  Title:Ruins
  @kankichi573
*/
#include <stdio.h>
#include <math.h>
#include <limits.h>
extern double sqrt(double);

int compare_int(const void *a, const void *b)
{
    return *(int*)b - *(int*)a;
}

int solve(int m,int n)
{
  int min_,sum,i,j,k,l,a[4],b[4];
  min_=INT_MAX;

  for(i=1;i<sqrt(m);i++)
    {
      if(m % i)
	continue;
      a[0]=i;
      a[1]=m/i;
      for(j=1;j<sqrt(n);j++)
	{
	  if(n % j)
	    continue;
	  a[2]=j;
	  a[3]=n/j;
	  for(k=0;k<4;k++)
	    b[k]=a[k];
	  qsort(b,4,sizeof(int),compare_int);
	  sum=0;
	  for(k=0;k<3;k++)
	    sum += (b[k]-b[k+1])*(b[k]-b[k+1]);
	  if(sum < min_)
	    {
	      /*
	      for(l=0;l<4;l++)
		printf("%d:",b[l]);
	      printf("\n");
	      */
	    min_ = sum;
	    }
	}
    }
  return(min_);
}
main()
{
  int N,M,ret;

  while(scanf("%d %d",&N,&M) && (N || M))
    {
      ret=solve(N,M);
      printf("%d\n",ret);
    }

  return(0);
}