import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int d = sc.nextInt(); System.out.println(getLCM(n, d) / d - 1); } static long getLCM(long x, long y) { return x / getGCD(x, y) * y; } static long getGCD(long x, long y) { if (x % y == 0) { return y; } else { return getGCD(y, x % y); } } }