#![allow(non_snake_case)] use std::io::{ self, prelude::* }; macro_rules! pick { ($tokens:expr) => { $tokens.next().unwrap().parse().expect("parse error") } } fn gcd(a: i64, b: i64) -> i64 { if b == 0 { a } else { gcd(b, a%b) } } fn main() { let mut s = String::new(); io::stdin().read_to_string(&mut s).expect("i/o error"); let mut tokens = s.split_whitespace(); let N: i64 = pick!(tokens); let D: i64 = pick!(tokens); let ans = N/gcd(N,D) - 1; println!("{}", ans); }