#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define ll long long int #define ti4 tuple #define pii pair #define REP(x,n) for(int x = 0;x < n;x++) template void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } struct UnionFind { vector data; UnionFind(int size) : data(size, -1) { } bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; const ll mod = (ll)(1e9+7); struct SimpleHash { size_t operator()(const std::pair& p) const { return p.first * mod + p.second; } }; ll gcd( ll a, ll b ) { ll c; while ( a != 0 ) { c = a; a = b%a; b = c; } return b; } ll lcm( ll m, ll n ) { if ( ( 0 == m ) || ( 0 == n ) ) return 0; return ((m / gcd(m, n)) * n); } int main() { ll n, d; cin >> n >> d; if(d > n) { d = d % n; } if(d == 0) { cout << 0 << endl; return 0; } if(n == d) { cout << 0 << endl; return 0; } ll l = lcm(d, n); cout << (l / d) - 1 << endl; return 0; }