#include using namespace std; int main(){ int n,d; cin >> n >> d; cout << n/__gcd(n,d)-1 << endl; return 0; }