Massachusetts has spent $20.6 million over the last five years responding to two scandals involving misconduct at state drug labs, according to the Executive Office of Administration and Finance.

The money covers the investigations, the prosecution of the two chemists responsible, and the costs of notifying defendants and dealing with thousands of drug cases tainted by the misconduct.

The Hinton Lab Response Reserve fund was set up in February 2013, through a supplemental budget bill, to respond to the case of Annie Dookhan. The former chemist pleaded guilty to falsifying the results of tests done on drugs at a state lab in Boston. Those tests were used to convict thousands of people of drug offenses. The fund was meant to cover the costs of that investigation.

The fund was expanded in March 2017 to also cover the investigation of cases tainted by Sonja Farak, who stole samples of drugs from a state lab in Amherst to feed her own addiction.

The state was responsible for not only investigating and prosecuting Farak and Dookhan but also for determining how many cases were tainted and dealing with those cases. The American Civil Liberties Union and others have brought extensive litigation, trying to get those defendants notified and have the cases dismissed.

The comptroller's public state spending website details $17.49 million in spending. Generally, some expenditures are left off the public site because they are considered confidential under state or federal law.

The biggest portion of money from the reserve fund went to investigatory agencies. The Office of the Inspector General received $5.77 million, according to the comptroller's data. Most of this went to information technology.

The Office of the Inspector General took over the investigation of the Dookhan case in 2013 because of the potential conflicts of interest of having then-Attorney General Martha Coakley prosecuting Dookhan while also investigating how many lab cases were tainted.

Jack Meyers, a spokesman for the Office of the Inspector General, said the office had to create databases to keep track of millions of drug tests that were run on samples and evidence. The office hired contractors and vendors with computer expertise to set up and maintain these databases. Other state law enforcement and health agencies also used the databases.

The inspector general's office was not involved in the Farak case.

State police got a little under $3 million, with most of that going toward infrastructure and equipment.

David Procopio, a state police spokesman, said after the Dookhan scandal, the police took over operations of the Hinton lab from the Department of Public Health and moved the drug testing to a state police lab in Sudbury. The state police used the money to expand the Sudbury lab, buy new scientific equipment and hire and train new chemists to take on the additional caseload.

The state police used some money to assist an attorney who was analyzing all the Hinton cases to determine which ones required additional judicial action.

According to Procopio, the state police also took over Farak's lab and moved those drug testing cases to a state police drug lab in Springfield. The cost of that move was absorbed into the existing state police budget, so no money from the trust fund was used.

The state reserve fund also paid for costs related to both the prosecution and the defense in the litigation.

The fund reimbursed the state's district attorneys for expenses related to the cases, spending anywhere from $1.95 million for the Suffolk district attorney's office to $11,000 for the Worcester district attorney.

Jake Wark, a spokesman for the Suffolk district attorney, said as a result of litigation, prosecutors had to notify defendants whose drug cases were tainted by Dookhan of their rights. The money went toward locating mailing address and mailing notices to defendants.

Suffolk County had 8,000 cases touched by Dookhan - by far the highest number in the state.

The reserve fund also reimbursed the Committee for Public Counsel Services, the state's public defender office, $1.67 million for staff time.

Lisa Hewitt, general counsel for the Committee for Public Counsel Services, said huge numbers of the drug cases tainted by Dookhan and Farak involved indigent defendants. Public defenders have had to look at hundreds of thousands of cases and relitigate those that were not dismissed by the district attorneys.

The public defenders, along with the ACLU, have asked that all the cases be dismissed. But judges have decided that prosecutors should instead use case-by-case analysis to determine which cases should be dismissed and which they can retry.

The public defenders have an in-house unit focused on these cases. They have also had to hire private appellate attorneys for some cases, Hewitt said.

Several state agencies were also reimbursed by the fund, including the governor's office ($625,000 for legal services, staff time and consultants), the Executive Office of Health and Human Services ($499,000), the Department of Public Health ($349,000) and the public safety and corrections departments (less than $100,000 each).

Spending from the fund peaked between fiscal 2013 and fiscal 2015, although the fund has continued to spend money through this year as litigation continues.