import java.util.*; class A{ static Scanner s = new Scanner(System.in); public static void main(String[] args){ int p = s.nextInt(), j = s.nextInt(); System.out.println(p/gcd(p,j)-1); } public static long gcd(long a, long b) { long m; while(a!=0) { m = Math.min(a, b); a = Math.max(a, b)%m; b = m; } return b; } }