logic in PT07B of spoj

I am not getting the logic of solving PT07B of spoj…Please help me out to get a correct logic…Thanks in advance

you are just asking for the solution. maybe explain us where you are going wrong