import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long a = sc.nextLong(); long b = sc.nextLong(); long ans = (long)Math.pow(10, 18) + 1; for(long i = 1; (i * i) <= (a + b); i++) { if(((a + b) % i) == 0) { long t = (a + b) / i; if((((b + i) % a) == 0) && (((a + i) % b) == 0)) { ans = Math.min(ans, i); } if((((b + t) % a) == 0) && (((a + t) % b) == 0)) { ans = Math.min(ans, t); } } } if(ans == ((long)Math.pow(10, 18) + 1)) ans = -1; System.out.println(ans); } }