import java.util.*; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) throws Exception { long n = sc.nextLong(); long d = sc.nextLong(); long m = gcd(n,d); System.out.println(n/m-1); } public static long gcd(long a, long b) { long tmp; while (b!=0) { tmp = a%b; a = b; b = tmp; } return a; } }