• + 1 comment

    I did pretty much that

    cout<<(S+M-2)%N+1<<"\n";
    

    But I didn't completely understand why it worked, your index explanation helped, thanks.