#!/usr/bin/ruby
while(a,b=gets.split.map(&:to_i))!=[0,0]do
	r=9999999
	1.step(Math.sqrt(a)){|i|
		i=i.to_i
		if a%i>0 then next end
		1.step(Math.sqrt(b)){|j|
			j=j.to_i
			if b%j>0 then next end
			x=[i,a/i,j,b/j].sort
			p=(x[3]-x[2])**2+(x[2]-x[1])**2+(x[1]-x[0])**2
			if r>p then r=p end
		}
	}
	p r
end