Little Pony and Sort by Shift

CodeForces - 454B