import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); long a = sc.nextInt(); long b = sc.nextInt(); long sum = a + b; if ((a + 1) % b == 0 && (b + 1) % a == 0) { System.out.println(1); return; } TreeSet remain = new TreeSet<>(); remain.add(sum); for (long i = 2; i <= Math.sqrt(sum); i++) { if (sum % i == 0) { if ((a + i) % b == 0 && (b + i) % a == 0) { System.out.println(i); return; } remain.add(sum / i); } } for (long x : remain) { if ((a + x) % b == 0 && (b + x) % a == 0) { System.out.println(x); return; } } System.out.println("-1"); } }