import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); long D = sc.nextLong(); long g = gcd(N, D); System.out.println(N / g - 1); } public static long gcd(long a, long b) { if(b == 0) return a; long M = Math.max(a, b); long m = Math.min(a, b); return gcd(m, M % m); } }