Grand temple Problem

Need some hint to solve the problem