import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from math import gcd N,D = map(int,read().split()) g = gcd(N,D) answer = N//g - 1 print(answer)