import sys input=sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 import math a,b=MI() if min(a,b)==1: print(0) exit() if math.gcd(a,b)!=1: print(-1) else: S=set([]) M=a*b for i in range(101): for j in range(101): temp=a*i + b*j if temp