How to solve BAHUDAN, Mahilmati contest

can any one please explain this question. How to find its solution.

https://www.codechef.com/problems/BAHUDAN