Lights New Car Doubt

I did not understand the part that how B= a * (p-1) + b and then how b= B%(p-1) ?


Do tell me if still unclear!

sir how are we getting B=y%(m-1) in last step ???