import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int sum = a + b; int min = Integer.MAX_VALUE; for (int i = 1; i <= Math.sqrt(sum); i++) { if (sum % i != 0) { continue; } if (i == a || i == b) { continue; } if (canSet(a, b, i)) { min = i; break; } if (sum / i == a || sum / i == b) { continue; } if (canSet(a, b, sum / i)) { min = Math.min(min, sum / i); } } if (min == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(min); } } static boolean canSet(int a, int b, int c) { return (a + c) % b == 0 && (b + c) % a == 0; } }