For Lori Stone, getting out of prison has always been a little nerve-racking.

She's been in and out of jail since she was 18. Every time she's been released, she's lost her disability benefits and her Medicaid coverage. That meant she couldn't afford her rent or her medication for her bipolar disorder until she was able to re-enroll, which could take weeks or months -- even if she went to all her appointments on time.

"That would put me into a bad spell of being depressed, and my moods would be bad," says Stone, 37, over the phone from the Douglas County Jail in Omaha, Nebraska. "And then I would end up doing something stupid like shoplifting to get alcohol. It's just a vicious cycle."

That critical gap in safety net programs, which has set Stone up for failure again and again, is a harsh reality for millions of people released from prison every year — and one that counties are now trying to get fixed.

Local jails and prisons are required to provide prisoners with adequate health care. But the interruption of federal and state programs inmates had been depending on can cause major problems, making it more likely that people will cycle in and out of jail.

Federal rules prohibit states from billing Medicaid for any inmate care unless the covered individual requires a hospital stay of at least 24 hours. They also cut off Social Security and Disability payments and some veterans' benefits. Medicaid benefits are taken away as soon as a suspect has been booked into jail, whether they've been proven guilty or not. If they are convicted and incarcerated, Social Security and VA benefits disappear 30 and 60 days later, respectively.

Some states simply suspend benefits, allowing inmates to pick them back up as soon as they're released. But 34 states still terminate enrollment either immediately or after the prisoner spends a certain period of time behind bars.

Related: The financial impact of winning (and losing) the birth lottery

With prisons and jails at the front lines of treating mental illness and the opioid epidemic, that has saddled local governments with billions of dollars in medical costs — $8.1 billion total in 2015, comprising roughly a fifth of correctional budgets, according to the Pew Charitable Trusts — and left their inmates lurching in and out of care.

For example: About 85% of the inmate population under the care of the Hillsborough County, New Hampshire Department of Corrections has some kind of substance abuse problem, says Superintendent David Dionne. Out of an average daily population of about 480 people, between one and two dozen are detoxing at any given time.

Dionne spent $330,000 last year on the medications for diseases like Hepatitis C, which is particularly common among drug users who share needles, along with patching up the physical sores that come along with injecting frequently.

The department contracted with an outside company to provide a 60-day in-house treatment program, but most inmates don't stay long enough to go through with it. And once they leave, treatment ends — at least until they manage to get covered again by Medicaid or other insurance.

"On the outside, if they didn't get their treatment, they'd come right back through the front door again," Dionne says.

In order to smooth the path back to the outside world and reduce the strain on state budgets, 16 states and Washington, D.C. simply suspend Medicaid benefits and automatically reinstate them upon the inmate's release, rather than terminating enrollment entirely. That ensures newly released prisoners have coverage immediately, since re-enrolling can take weeks or months.

Nebraska passed a law allowing for suspension rather than termination in 2015, which means that this time, Lori Stone will be able to seamlessly regain her benefits and join a long-term treatment program once she gets out.

But even in states that automatically reinstate federal benefits, the interruption disrupts lives.

Another Douglas County inmate, Julia Conger, is a single mother of four children who was jailed in January for unlawful possession of someone else's debit card. She had been supporting her kids with her disability benefits, and they also received health care through her Medicaid coverage — both of which ceased when Conger went to jail.

When her sister took Conger's daughter to the dentist for a tooth problem, she was no longer covered. (Although new guardians can transfer Medicaid benefits to children in their care, it doesn't happen immediately.)

"It's causing my daughter pain," Conger says. "And it's terrible for me, not being able to do anything."

Given the expansion of Medicaid under the Affordable Care Act, the cost of health care in jails is a particularly large lost opportunity for local governments. If it weren't for the Medicaid and Social Security rules, the federal government would cover at least half of their expenses, which would especially be a relief for cash-strapped rural counties that have to bring in doctors from elsewhere and may not have the funding for expensive drug treatment programs.

"They're going to be in crisis in a few years unless we find some alternative," says Ron Manderscheid, who heads the National Association of County Behavioral Health and Developmental Disability Directors. "What is the trade-off between operating our schools and spending this huge amount of money on the county jails for health care?"

Related: In a small Kentucky coal town, joblessness leads to a health crisis

But spending more on health care for those who've run afoul of the law is a hard sell in Washington.

In 2017, legislators introduced a bill that would at least maintain Medicaid eligibility for pretrial inmates, who accounted for 65% of the 740,700 people in jails in 2016, according to the Bureau of Justice Statistics. Another bill would require states to suspend rather than terminate benefits for juvenile inmates. A third would allow counties to reinstate Medicaid for inmates 30 days before their release.

None of those bills have progressed beyond introduction. The one that appears to have the best chance of passing is a measure sponsored by Ohio Republican Rep. Mike Turner that would allow Medicaid to cover an inmate's substance abuse treatments.

"Where we're seeing current traction is the opioid epidemic," says Brian Bowden, a health policy expert with the National Association of Counties. "If you were starting folks on treatment before they're released, then you're not having that revolving cycle."