# -*- coding: utf-8 -*- import math N,D = map(int, input().split()) G = math.gcd(N,D) print(N//G - 1)