loop {
    a, b = gets.split.map &:to_i
    break if a == 0
    a_pairs = []
    (1..Math.sqrt(a).to_i).each{|x|
        a_pairs << [x, a / x] if a % x == 0
    }
    b_pairs = []
    (1..Math.sqrt(b).to_i).each{|x|
        b_pairs << [x, b / x] if b % x == 0
    }
    min = Float::INFINITY
    a_pairs.each{|x, y|
        b_pairs.each{|z, w|
            s = [x,y,z,w].sort.each_cons(2).map{|s, t| (s-t)**2}.inject(:+)
            min = s if s < min
        }
    }
    p min
}