#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; ll gcd(ll a, ll b){return (b==0?a:gcd(b,a%b));} ll lcm(ll a, ll b){return a/gcd(a,b)*b;} int main(){ ios::sync_with_stdio(false); ll N, D; cin >> N >> D; if(gcd(N,D)==1){ cout << N-1 << endl; }else{ cout << lcm(N,D)/D-1 << endl; } return 0; }