Need help figuring out why this code is getting TLE. Problem link : 1829D - Gold Rush
void solve1829D()
{
int n, m, cnt = 0;
cin >> n >> m;
set<int> st;
st.insert(n);
while(st.size() > 0)
{
if(st.find(m) != st.end())
{
cout << "YES" << endl;
return;
}
int x = *st.begin();
st.erase(st.begin());
if(x % 3 == 0) {st.insert(x / 3); st.insert((x * 2) / 3);}
}
cout << "NO" << endl;
}