Come to prayer with calmness and tranquility. Then pray the part you get (long with the imam) and complete afterwards the part you miss. Abu Dawud said: Ibn Sirin narrated from Abu Hurairah the words: "he should complete it afterwards." Similarly, Abu Rafi' narrated from Abu Hurairah and Abu Dharr narrated from him the words "then complete it, and complete it afterwards." There is a variation of words in the narration from him.
عَنِ النَّبِيِّ ﷺ قَالَ ائْتُوا الصَّلاَةَ وَعَلَيْكُمُ السَّكِينَةُ فَصَلُّوا مَا أَدْرَكْتُمْ وَاقْضُوا مَا سَبَقَكُمْ