defmodule Main do def divisors(n, curr) when curr > n, do: [] def divisors(n, curr) when rem(n, curr) == 0, do: [curr] ++ divisors(div(n, curr), curr + 1) def divisors(n, curr), do: divisors(n, curr + 1) def find([], _, _), do: -1 def find([x|xs], a, b) do cond do rem(x + a, b) == 0 && rem(x + b, a) == 0 -> x true -> find(xs, a, b) end end def solve(a, b) do divisors(a + b, 1) |> find(a, b) end def main do [a, b] = IO.gets("") |> String.trim |> String.split |> Enum.map(&String.to_integer/1) solve(a, b) |> IO.puts end end