Chef and beautiful number march challenge

https://www.codechef.com/viewsolution/23550655 this is my solution. It is working for mine every input. I don’t know why it gives WA. please help.