Looking at the steps we can see that this is actually a Gray code (Reflected Binary Code) counting from the original value down to 0. So if we generate a list of sufficient codes to cover the starting value, we can simply look for the binary representation of the starting value in that list, and that will give us the number of steps required to get back to 0: