In August 2016, U.S. citizen Kevin King and his Australian colleague, Timothy Weeks, were driving through central Kabul when gunmen ambushed their SUV.

Then both men, who were in the Afghan capital to teach English at the American University of Afghanistan, disappeared.

More than three years after their abduction, the men were freed Tuesday in exchange for the release of three insurgent commanders of the Haqqani network, a group closely allied with the Taliban.

In a speech announcing the deal last week, Afghan President Ashraf Ghani said the American and Australian men’s health had deteriorated in Taliban custody. King is 63, and Weeks is 50.

Immediately after their abduction, the Afghan government said they appeared to have been kidnapped by a criminal gang. But the next month, the Pentagon said that Navy SEALs had attempted to rescue the two men from Taliban captivity in eastern Afghanistan but that the raid was unsuccessful.

Four months later, in January 2017, King and Weeks appeared in a Taliban propaganda video, begging to be released.

Both men seemed unwell in the emotional 13-minute video, where they pleaded for the U.S. government to negotiate a prisoner swap to win them their freedom.

In the video, King said U.S. officials “can exchange us for some prisoners in Bagram,” referring to a military prison in Afghanistan where many Taliban insurgents are held. All three commanders released Tuesday were being held near Bagram air base.

King’s hair and beard had grown long, and he coughed repeatedly in the video and rubbed his eyes as he listed the names of his family members. At one point, as King described the details of their case, Weeks leaned over his knees with his head in his hands, wiping tears from his eyes. King then begged again for the U.S. government to talk to the militants about a prisoner swap. “We don’t know how much longer the Taliban will be patient,” he said.

Later that year, the Taliban warned that King was seriously ill with kidney and heart disease. The group said in a statement at the time that they tried to care for him but didn’t have the right facilities “since we are facing war conditions.”

“The condition of . . . said teacher has exponentially worsened,” the statement said.

The FBI offered a reward of up to $1 million for information that would lead to King’s release.

Criminal kidnappings are relatively common in Kabul, where gangs often pass abductees to the Taliban, which then uses them as bargaining chips.

King and Weeks were abducted from close to the American University of Afghanistan’s campus, in a busy part of Kabul. The university, founded in 2006, is Afghanistan’s first private, nonprofit university. King started teaching there in 2014 and Weeks had only started his role about a month before the kidnapping took place, the university said in a statement calling for their release shortly after their abduction.

In a statement Tuesday, the university expressed relief over the news of the release of the two men. “We look forward to providing all the support we can to Kevin and Tim and their families,” it said.