n,d = map(int,input().split())
def cal(x,y):
    if y == 0:
        return x
    return cal(y,x%y)


t=cal(n,d)

if n == d:
    print(0)
elif t == 1:
    print(n-1)
else:
    print(t-1)