package No400番台; import java.util.Scanner; public class Q415 { public static void main(String[] args) { new Q415().solver(); } void solver() { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); long D = sc.nextLong(); System.out.println(N / Math.abs(gcd(D, N)) - 1); } static long gcd(long t1, long t2) { if (Math.abs(t1) < Math.abs(t2)) { long d = t2; t2 = t1; t1 = d; } if (t2 == 0) return t1; return gcd(t2, remainder(t1, t2)); } static long remainder(long a, long b) { long r = a % b; if (Math.abs(r) > Math.abs(b / 2)) { if (r > 0 && b > 0) { r -= b; } else if (r > 0 && b < 0) { r += b; } else if (r < 0 && b > 0) { r += b; } else if (r < 0 && b < 0) { r -= b; } else { throw new AssertionError(a + " " + b); } } return r; } }