require 'prime' require 'set' def _divisors(primes, k) Enumerator.new do |y| if primes.size == k y << 1 else p, e = primes[k] _divisors(primes, k + 1).each do |d| (e + 1).times { |e1| y << p ** e1 * d } end end end end def divisors(n) _divisors(Prime.prime_division(n), 0).to_a end a, b = gets.split(" ").map(&:to_i) ab = a + b list = divisors(ab) c = -1 list.each do |i| next if i == a || i == b ac, bc = a + i, b + i if ac % b == 0 && bc % a == 0 c = i break end end puts c