Here is the way to figure it out. First re-read the verse in Matthew.
Matthew 1:17 So all the generations from Abraham to David are fourteen generations; and from David unto the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations.
And count the way the text reads:
generations from Abraham to David are fourteen generations;
Abraham - Isaac - Jacob - Judas - Phares - Esrom - Aram - Aminadab - Naasson - Salmon - Booz - Obed - Jesse - David = 14 generations
from David unto the carrying away into Babylon are fourteen generations;
David:..Solomon - Roboam - Abia - Asa - Josaphat - Joram - Ozias - Joatham - Achaz - Ezekias - Manasses - Amon - Josias - - (carried away to Babylon) = 14 generations
From Jechonias (carrying away):
Jechonias - Salathiel - Zorobabel - Abiud - Eliakim - Azor - Sadoc - Achim - Eliud - Eleazar - Matthan - Jacob - Joseph (husband of Mary) - Christ = 14 generations.
Matthew 1:17 So all the generations from Abraham to David are fourteen generations; and from David unto the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations.
The text is not 3 times 14, David is the end of one count and David is the beginning of the second 14.
here is the scripture
1 The book of the generation of Jesus Christ, the son of aDavid, the son of Abraham.
2 aAbraham begat Isaac; and Isaac begat Jacob; and Jacob begat bJudas and his brethren;
3 And Judas begat aPhares and bZara of Thamar; and Phares begat cEsrom; and Esrom begat Aram;
4 And Aram begat Aminadab; and Aminadab begat Naasson; and aNaasson begat Salmon;
5 And Salmon begat aBooz of Rachab; and Booz begat Obed of Ruth; and Obed begat Jesse;
6 And Jesse begat David the king; and David the king begat aSolomon of her that had been the wife of Urias;
7 And Solomon begat Roboam; and Roboam begat Abia; and Abia begat Asa;
8 And Asa begat Josaphat; and Josaphat begat Joram; and Joram begat Ozias;
9 And Ozias begat Joatham; and Joatham begat Achaz; and Achaz begat Ezekias;
10 And Ezekias begat Manasses; and Manasses begat Amon; and Amon begat Josias;
11 And Josias begat Jechonias and his brethren, about the time they were carried away to aBabylon:
12 And after they were brought to Babylon, Jechonias begat Salathiel; and Salathiel begat aZorobabel;
13 And Zorobabel begat Abiud; and Abiud begat Eliakim; and Eliakim begat Azor;
14 And Azor begat Sadoc; and Sadoc begat Achim; and Achim begat Eliud;
15 And Eliud begat Eleazar; and Eleazar begat Matthan; and Matthan begat Jacob;
16 And Jacob begat aJoseph the husband of bMary, of whom was born cJesus, dwho is called eChrist.
verses 2-6.5 = 14 generations
verses 6.5-11 = 14 generations
verses 12-16 = 13 generations