import java.util.*; public class T02 { static int gcd(int a, int b) { if ( b > a) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int D = sc.nextInt(); if ( N / 2 < D) { D = N - D; } if (D == 0) { System.out.println(0); } else if (gcd(N, D) > 1) { System.out.println(N / gcd(N, D) - 1); } else { System.out.println(N - 1); } } }