import scala.io.StdIn.readLine
import scala.collection.mutable.ArrayBuffer

object Main {
  def decompose(n:Int) = {
    val a = new ArrayBuffer[(Int,Int)]
    for(i<-1 to Math.sqrt(n).toInt) {
      if(n%i==0)
        a += ((i,n/i))
    }
    a.toArray
  }
  def main(args:Array[String]) = {
    var s = "0 0"
    while({s=readLine.trim;s} != "0 0") {
      val n = s.split(" ").map(_.toInt)
      val x = decompose(n(0))
      val y = decompose(n(1))
      var min = Int.MaxValue
      for(i<-x) {
        for(j<-y) {
          val z = Array(i._1,i._2,j._1,j._2).sorted
          val sum = (z(1)-z(0))*(z(1)-z(0)) + (z(2)-z(1))*(z(2)-z(1)) + (z(3)-z(2))*(z(3)-z(2))
          if(sum<min) min = sum
        }
      }
      println(min)
    }
  }
}