How to solve NOVICE65 problem on SPOJ?

Here is the link regarding the problem NOVICE65 on SPOJ - http://www.spoj.com/problems/NOVICE65/

//