If a number is visited before, then the set that starts at this number must be smaller then

previous max. So we can safely skip this number

Actually, this should be:

If a number is visited before, then the set that starts at this number must be equal to previous max. So we can safely skip this number

This is because all connected numbers form a ring and no matter which number of this ring you start with total count will always be same.