import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Comparator; import java.util.HashSet; import java.util.NoSuchElementException; import java.util.Objects; import java.util.PriorityQueue; import java.util.TreeSet; import java.util.function.BiFunction; public class Main{ public static void main(String[] args){ FastScanner sc = new FastScanner(); Mathplus mp = new Mathplus(); PrintWriter out = new PrintWriter(System.out); int min = 2000000001; int A = sc.nextInt(); int B = sc.nextInt(); for(int i=1;i*i<=(A+B);i++) { if((A+B)%i==0) { int D = (A+B)/i; if(isOK(A,B,i)) min=Math.min(min, i); if(isOK(A,B,D)) min=Math.min(min, D); } } System.out.println(min==2000000001?-1:min); } private static boolean isOK(int a, int b, int i) { if(a==i||b==i)return false; return (a+i)%b==0&&(b+i)%a==0; } } class SegmentTree{ int N; BiFunction f; BiFunction g; T d1; ArrayList dat; SegmentTree(BiFunction F,BiFunction G,T D1,T[] v){ int n = v.length; f = F; g = G; d1 = D1; init(n); build(v); } void init(int n) { N = 1; while(N(); } void build(T[] v) { for(int i=0;i<2*N;i++) { dat.add(d1); } for(int i=0;i=0;i--) { dat.set(i,f.apply(dat.get(i*2+1),dat.get(i*2+2))); } } void update(int k,E a) { k += N-1; dat.set(k,g.apply(dat.get(k),a)); while(k>0){ k = (k-1)/2; dat.set(k,f.apply(dat.get(k*2+1),dat.get(k*2+2))); } } T query(int a,int b, int k, int l ,int r) { if(r<=a||b<=l) return d1; if(a<=l&&r<=b) return dat.get(k); T vl = query(a,b,k*2+1,l,(l+r)/2); T vr = query(a,b,k*2+2,(l+r)/2,r); return f.apply(vl, vr); } T query(int a,int b){ return query(a,b,0,0,N); } } class LazySegmentTree extends SegmentTree{ BiFunction h; BiFunction p = (E a,Integer b) ->{return a;}; E d0; ArrayList laz; LazySegmentTree(BiFunction F,BiFunction G,BiFunction H,T D1,E D0,T[] v){ super(F,G,D1,v); int n = v.length; h = H; d0 = D0; Init(n); } void build() { } void Init(int n){ laz = new ArrayList(); for(int i=0;i<2*N;i++) { laz.add(d0); } } void eval(int len,int k) { if(laz.get(k).equals(d0)) return; if(k*2+1t) { return i; }else { return find(t,root[i]); } } public void unite(int x,int y,int t) { now = t; x = find(t,x); y = find(t,y); if(x==y)return; if(rank[x]1) { int mid = (ok+ng)/2; if(find(mid,x)==find(mid,y)) { ok = mid; }else { ng = mid; } } return ok; } } class Graph { ArrayList[] list; int size; TreeSet Edges = new TreeSet(new LinkEdgeComparator()); @SuppressWarnings("unchecked") Graph(int N){ size = N; list = new ArrayList[N]; for(int i=0;i(); } } void addEdge(int a,int b){ list[a].add(new Edge(b,1)); } void addWeightedEdge(int a,int b,long c){ list[a].add(new Edge(b,c)); } void addEgdes(int[] a,int[] b){ int size = a.length; for(int i=0;i Q = new ArrayDeque(); Q.add(s); while(!Q.isEmpty()){ int v = Q.poll(); for(Edge e:list[v]){ int w = e.to; long c = e.cost; if(L[w]==-1){ L[w] = L[v] + c; Q.add(w); } } } return L; } long[] dijkstra(int s){ long[] L = new long[size]; for(int i=0;i Q = new PriorityQueue(new SampleComparator()); Q.add(new Pair(0,s)); while(!Q.isEmpty()){ Pair C = Q.poll(); if(visited[(int)C.b]==0){ L[(int)C.b] = C.a; visited[(int) C.b] = 1; for(Edge D:list[(int) C.b]){ Q.add(new Pair(L[(int)C.b]+D.cost,D.to)); } } } return L; } long[] maxtra(int s,long l){ long[] L = new long[size]; for(int i=0;i Q = new PriorityQueue(new SampleComparator()); Q.add(new Pair(l,s)); while(!Q.isEmpty()){ Pair C = Q.poll(); if(visited[(int)C.b]==0){ L[(int)C.b] = C.a; visited[(int) C.b] = 1; for(Edge D:list[(int) C.b]){ Q.add(new Pair(Math.max(L[(int)C.b],D.cost),D.to)); } } } return L; } long Kruskal(){ long ans = 0; for(int i=0;i=0&&e.b>=0) { if(!UF.same(e.a,e.b)){ ans += e.L; UF.unite(e.a,e.b); } } } return ans; } ArrayList Kahntsort(){ ArrayList ans = new ArrayList(); PriorityQueue q = new PriorityQueue(); int[] in = new int[size]; for(int i=0;i0)return new ArrayList(); } return ans; } Graph dfsTree(int i) { int[] used = new int[size]; Graph r = new Graph(size); dfsTree(i,used,r); return r; } private void dfsTree(int i, int[] used, Graph r) { used[i] = 1; for(Edge e:list[i]) { if(used[e.to]==0) { r.list[i].add(e); used[e.to] = 1; dfsTree(i,used,r); } } } } class Tree extends Graph{ public Tree(int N) { super(N); } long[] tyokkei(){ long[] a = bfs(0); System.out.println(); int maxdex = -1; long max = 0; for(int i=0;i{ public int compare(LinkEdge P, LinkEdge Q) { long temp = P.L-Q.L; if(temp==0){ if(P.a>Q.a){ return 1; }else{ if(P.b>Q.b){ return 1; }else{ return -1; } } } if(temp>=0){ return 1; }else{ return -1; } } } class Pair{ long a; long b; Pair(long p,long q){ this.a = p; this.b = q; } public boolean equals(Object o){ Pair O = (Pair) o; if(O.a==this.a&&O.b==this.b){ return true; }else{ return false; } } public int hashCode(){ return Objects.hash(a,b); } } class SampleComparator implements Comparator{ public int compare(Pair P, Pair Q) { long temp = P.a-Q.a; if(temp==0){ if(P.b>Q.b){ return 1; }else{ return -1; } } if(temp>=0){ return 1; }else{ return -1; } } } class LongIntPair{ long a; int b; LongIntPair(long p,int q){ this.a = p; this.b = q; } public boolean equals(Object o){ Pair O = (Pair) o; if(O.a==this.a&&O.b==this.b){ return true; }else{ return false; } } public int hashCode(){ return Objects.hash(a,b); } } class LongIntSampleComparator implements Comparator{ public int compare(LongIntPair P, LongIntPair Q) { long temp = P.a-Q.a; if(temp==0){ if(P.b>Q.b){ return 1; }else{ return -1; } } if(temp>=0){ return 1; }else{ return -1; } } } class FastScanner { private final java.io.InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return (minus ? -n : n); }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return (int) (minus ? -n : n); }else{ throw new NumberFormatException(); } b = readByte(); } } } class Mathplus{ int mod = 1000000007; long[] fac = new long[1000001]; boolean isBuild = false; int mindex = -1; int maxdex = -1; void buildFac(){ fac[0] = 1; for(int i=1;i<=1000000;i++){ fac[i] = (fac[i-1] * i)%mod; } isBuild = true; } public int isBigger(int[] d, int i) { int ok = d.length; int ng = -1; while(Math.abs(ok-ng)>1) { int mid = (ok+ng)/2; if(d[mid]>i) { ok = mid; }else { ng = mid; } } return ok; } public int isSmaller(int[] d, int i) { int ok = -1; int ng = d.length; while(Math.abs(ok-ng)>1) { int mid = (ok+ng)/2; if(d[mid] primetable(int m) { HashSet pt = new HashSet(); for(int i=2;i<=m;i++) { boolean b = true; for(int d:pt) { if(i%d==0) { b = false; break; } } if(b) { pt.add(i); } } return pt; } long max(long[] a){ long max = 0; for(int i=0;ia[i]){ min =a[i]; mindex = i; } } return min; } int min(int[] a){ int min = Integer.MAX_VALUE; for(int i=0;ia[i]){ min =a[i]; mindex = i; } } return min; } long sum(long[] a){ long sum = 0; for(int i=0;i