I am not able to solve problem "once upon a time" from this(http://codeforces.net/gym/100963/attachments) set . I tried extended euclidean algorithm. Can anyone provide the solution (in form of code or editorial) for this problem Or help me debug my code (http://codeforces.net/gym/100963/attachments) .