import java.io.InputStream; import java.io.PrintWriter; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; import static java.util.Comparator.*; public class Main { public static void main(String[] args) { PrintWriter out = new PrintWriter(System.out); Solver solver = new Solver(System.in, out); solver.solve(); out.close(); } } class Solver { Scanner sc; PrintWriter out; public Solver(InputStream in, PrintWriter out) { sc = new Scanner(in); this.out = out; } // ================================================================== public void solve() { int A = Integer.parseInt(sc.next()); int B = Integer.parseInt(sc.next()); int wk; Set set = new TreeSet(); for (int i = 1; i*i <= (A + B); i++) { if((A+B) % i != 0) continue; wk = (A+B) / i; if(wk == A || wk == B) continue; set.add(wk); set.add(i); } int ans = -1; for(int C : set) { if((A+B) % C == 0 && (B+C) % A == 0 && (A+C) % B == 0) { ans = C; // out.println("条件に一致した  ans = " + C); } } out.println(ans); } // ================================================================== }