CVS Health Corp, Walgreens Boots Alliance Inc. and Walmart Inc. have tentatively agreed to pay about $13.8 billion to resolve thousands of U.S. state and local government lawsuits accusing the chains of mishandling opioid painkillers, according to two people familiar with the negotiations.

The proposed settlement calls for CVS to pay $5 billion over 10 years, Walgreens to pay $5.7 billion over 15 years and Walmart to pay $3.1 billion, mostly up front, according to the people. The sources declined to be identified, saying they were not authorized to speak publicly about the matter.

CVS announced its portion of the deal, which includes $4.9 billion for state, county and city governments and $130 million for Native American tribes, Wednesday morning.

“We are pleased to resolve these longstanding claims, and putting them behind us is in the best interest of all parties, as well as our customers, colleagues and shareholders,” CVS general counsel Thomas Moriarty said in a statement. The company did not admit wrongdoing.

Walmart and Walgreens declined to comment. A spokesperson for the plaintiffs’ attorneys in the litigation did not immediately respond to a request for comment.

The proposed settlement, which would be the first nationwide deal with retail pharmacy companies, follows nationwide opioid settlements with drugmakers and distributors totaling more than $33 billion.

In more than 3,300 lawsuits, beginning in 2017, state and local governments accused drugmakers of downplaying the risks of their opioid pain medicines, and distributors and pharmacies of ignoring red flags that prescriptions were being diverted into illegal trafficking.

They said the resulting human toll, as well as strain on public health services and law enforcement, was a public nuisance that the companies must pay to fix.

CVS, Walgreens and Walmart are the three largest retail pharmacies in the country by market share. If their settlement becomes final, it will put much of the sprawling, years-long litigation over opioids to rest, though cases are still pending against smaller, more regionally focused pharmacy operators including Rite Aid Corp. and Kroger Co.

Plaintiffs had scored some significant trial victories against pharmacy chains, including a $650.6 million judgment in favor of two Ohio counties against CVS, Walgreens Boots Alliance and Walmart, and a ruling that Walgreens contributed to the opioid epidemic in San Francisco.

Previous settlements netted $21 billion from the three largest U.S. drug distributors, $5 billion from Johnson & Johnson, $4.35 billion from Teva Pharmaceutical Industries Ltd, $2.37 billion from AbbVie Inc. and $450 million from Endo International Plc.

Purdue Pharma LP, whose prescription pill OxyContin is widely blamed for sparking the addiction and overdose crisis, and its Sackler family owners are seeking to resolve opioid claims against them through a $6 billion settlement in bankruptcy court.

State and local authorities have said they will use the money from the settlements to combat the opioid crisis, which according to federal government data has caused nearly 650,000 overdose deaths since 1999 and is continuing to worsen.

Prescriptions for opioids rose sharply in the 1990s as companies aggressively promoted the drugs, long used primarily in cancer patients, as a safe way to treat all kinds of chronic pain.

Overdoses involving opioids, including prescription pills and heroin, surged further during the COVID-19 pandemic, increasing 38 percent in 2020 over the previous year and another 15 percent in 2021, according to the U.S. Centers for Disease Control and Prevention.

The agency has attributed much of the recent rise in overdose cases to illegally manufactured fentanyl, a powerful synthetic opioid.

A congressional report last month put the economic toll of the opioid crisis in 2020 alone at $1.5 trillion.

(Reporting by Baranjot Kaur in Bengaluru, Brendan Pierson in New York and Nate Raymond in Boston; editing by Alexia Garamfalvi, Kenneth Maxwell and Mark Potter)