Solution to Sherlock and The Beast problem.