Please go through the link below:
https://ttlc.intuit.com/questions/3144968-how-to-calculate-the-magi
Hope this helps.