one—and that, too, poses no difficulty.
However, if the two prayers differ in length—so that the follower’s prayer is two rakʿahs while the imam’s is four, or the reverse; or the follower’s is three while the imam’s is four, or the reverse—then we proceed as follows.
If the follower’s prayer is the longer of the two, there is no issue. Consider a man who enters the mosque intending to perform Maghrib. Once the iqāmah is called, he remembers that he had prayed ʿAsr without wudu, so ʿAsr is now due. We say to him: “Join the congregation intending ʿAsr; when the imam gives the salām, stand and add one rakʿah to complete your four.” This entails no problem at all.
If, on the other hand, the imam’s prayer is longer than the follower’s, then joining in the imam’s second rakʿah or later is likewise free of difficulty. The complication only arises when the follower joins from the very first rakʿah. Let us give an example.
You arrive while the imam is leading ʿIshāʾ—something that happens often on rainy Fridays when people come from home to a central mosque. You find them in the last two rakʿahs. In that case, enter the prayer intending Maghrib, perform those two rakʿahs with the imam, and when he concludes, rise and add one rakʿah. No problem whatsoever.
If you arrive and find them praying the late ʿIshāʾ but they are already in the second rakʿah, we say: “Join them intending Maghrib and finish with the imam; you will have neither added nor omitted anything.” This, too, is perfectly sound, although some people find it puzzling. They argue: “If I join in the second rakʿah, I will sit for the tashahhud in what is my first rakʿah but the imam’s second; it looks as though I am sitting for tashahhud after only one rakʿah.”