from math import * A, B = map(int, input().split()) if gcd(A, B) != 1: print(-1) exit() ans = 0 now = 0 S = set() while True: if now % B in S: print(ans) exit() S.add(now % B) ans += now // B now += A