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 / gcd(D, N) -1); } static long gcd(long a, long b) { int e = 0; while ((a & 1) == 0 && (b & 1) == 0) { a >>= 1; b >>= 1; e++; } a = reduce(a); b = reduce(b); while (b != 0 && b != 1) { long c = Math.abs(a - b); a = Math.min(a, b); b = c; } if (b == 0) { return (1 << e) * a; } else if (b == 1) { return 1 << e; } else { throw new AssertionError(b); } } static long reduce(long a) { while ((a & 1) == 0) { a >>= 1; } return a; } }