Brazilian authorities are scrambling to stop a wave of prison violence that has killed at least 125 inmates in two weeks, many with their heads cut off or their hearts and intestines ripped out.

Three massacres and many smaller fatal clashes, all in northern states, have followed a pattern that security forces have been unable to get ahead of. It starts with an hours-long riot inside a penitentiary, then military police are sent in to assist severely outnumbered guards and distraught families wait outside while inmates post on social media photos they take of chopped up body parts.

The factors fueling the violence among rival gang members are so complex that authorities say some potential remedies could exacerbate the problem. And the possibility of continued killings is raising fears the violence could spill into favelas, or slums, in major cities like Rio de Janeiro and Sao Paulo, where drug operations are run in conjunction with bosses in prisons.

"Stopping the bloodshed begins with the federal and state governments calling this what it is: a public emergency," said Robert Muggah, research director for the Igarape Institute, a Brazil-based think tank that focuses on security. "This is a problem from hell that has been festering for half a century."

President Michel Temer was initially slow to respond, taking three days to publicly acknowledge the first massacre, and he was ridiculed for calling it a "terrible accident." But the government has since announced a series of measures aimed at stopping the violence.

National guard troops are being dispatched to hot spots, thousands of inmates are being shuffled among prisons and Temer has announced plans to build five new lockups. Meanwhile, justice authorities are examining ways to speed up a legal process that keeps thousands locked up for months or years while their cases are processed.

Nothing seems to be working so far. After several days of relative calm, 26 inmates were killed late Saturday in clashes inside two adjoining prisons in the northern state of Rio Grande do Norte. Authorities said members of the Sao Paulo-based First Command, Brazil's largest criminal gang known by the Portuguese acronym PCC, fought with local gang Crime Syndicate of Rio Grande do Norte.

Authorities said military police would not enter the prisons until daylight Sunday because it was too dangerous.

"There are more than 1,000 prisoners, all out of their cells, and some are armed," military police Maj. Wellington Camilo da Silva told reporters. "What we are doing now is trying to avoid a massive escape."

Prisoners returned to their cells by Sunday, and police found 26 bodies, many of them decapitated.

The wave of violence began with the arrival of the new year, when 56 inmates were killed in the northern state of Amazonas. Authorities said the Family of the North gang targeted members of the First Command in a clash over control of drug-trafficking routes in northern states. Many were decapitated, their heads tossed outside prison walls.

Then on Jan. 6, in the neighboring state of Roraima, 33 prisoners were killed, many with their hearts and intestines ripped out. Authorities said First Command members instigated the bloodshed against other prisoners for motives not entirely clear.

Ten other inmates were killed in smaller prison clashes in Amazonas and the northeastern state of Paraiba during the first few days of the month.

The central problem that authorities are trying to address is extreme overcrowding, which exacerbates squalid conditions that include infestations of rats and poisonous centipedes.

Brazil had 622,000 prisoners in 2014, compared to 233,000 in 2000, according to Ministry of Justice data. Brazil's prisons combined were built to house roughly half the current number.

While new facilities would help ease overcrowding, it could take years to build them.

A more realistic option would be to reduce the number be people behind bars, experts say. Several studies have found that around 40 percent of Brazil's inmates have not even been convicted and are in cells waiting for their cases to be heard. Many of them survive by joining gangs, which recruit members while providing protection and financial support.

Last week, Chief Supreme Court Justice Carmen Lucia called on authorities in each state to undertake an expedited analysis of all inmates who have not been convicted. Lucia said the idea wasn't to release people, but determine cases in which house arrest would be feasible.

Any large-scale release would likely be met with sharp resistance from politicians and citizens, however. "A good criminal is a dead criminal" is a common phrase in a country where violence permeates daily life.

Prison authorities have been moving thousands of prisoners in a kind of cat-and-mouse game aimed at stopping gang leaders from ordering new hits and saving inmates who could be targeted. But that strategy also carries risks. Prisoners not associated with a gang can be exposed to dangers in new prisons, while veteran gang members can court fresh recruits.

Many experts argue that the way authorities have handled First Command members behind bars over the past decade is at the root of the latest bloodshed.

"Transferring PCC leaders to other states made the group even more national," said Graham Denyer Willis, a lecturer at Cambridge University and author of "The Consensus Killing," on police and First Command violence in Sao Paulo. "It was a wrong policy then and it is still now."


Peter Prengaman on Twitter: http://www.twitter.com/peterprengaman

Mauricio Savarese on Twitter: http://www.twitter.com/MSavarese