Detecting Cycles solution