#!/usr/bin/env python3 import math n, d = map(int,input().split()) print(n // math.gcd(n, d) - 1)