I could answer based on EO Tradition, but I don't really want to open that can of worms. Based purely on the text it seems most likely that Matthew has not included every single name in his list. He framed his genealogy in three parts each with 14 generations. For him the number was a more important message than the names in between. If you start at David who is undisputedly the same person in both, Matthew has 28 names to Jesus and Luke has 42. You could explain some of this with late child bearing to a point, but I think it's unlikely. Another plausible explanation would be that the different names were the same people - many people used multiple names, although Heli - Jacob doesn't seem like a likely combination. A third explanation would be that Joseph is a levirate son, which would mean that Heli and Jacob were brothers, one died leaving a childless widow who, according to the Law, was then taken by the brother. I think any of the explanations would honor the text without distorting the message. On the other hand, saying that either of the genealogies is through Mary seems to be a direct contradiction to the text.