import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); long D = sc.nextLong(); if(N < D){ long temp = N; N = D; D = temp; } System.out.println(N/gcd(N,D)-1); } public static long gcd(long N, long D){ while(D != 0){ long r = D; D = N % D; N = r; } return N; } }