How to solve CHEFAFD ?

Can someone explain how to solve CHEFAFD ? I can’t think of anything other than brute force for this.

Link : https://www.codechef.com/DEC16/problems/CHEFAFD

2 Likes
//