CVS Pharmacy Inc. and Walgreens Boots Alliance Inc. agreed to pay more than $10 billion to settle thousands of lawsuits by states, cities and counties alleging the retailers contributed to the nation’s opioid crisis.

CVS agreed to pay $4.9 billion over 10 years, which includes $642 million in attorney fees and costs. CVS also said it would pay $130 million to Native American tribes.