capital gains

I purchased a mutual fund many years ago and purchased additional shares a few times. I have those share prices and gross purchase prices for those transactions. Since the first purchase I have always reinvested dividends which occurred at varying share prices. When I sell shares in the fund how do I account for all those reinvested dividends to calculate capital gains?