America is coming to the belated realization that it has a heroin epidemic on its hands. And this problem is intertwined with a larger epidemic of legal painkillers, which killed more people than car crashes or guns in 2014.

Like all big social changes, part of this story is economic. How much money people make, where, how, and whether they can work, what they can buy — all of this forms the superstructure in which human lives and communities grow and thrive or whither and die. That applies to the heroin epidemic as much as anything else.

And like all economic stories, this one comes in two parts: The demand side and the supply side. The demand side — why so many more Americans are using — has gotten most of the attention so far. But the supply side is equally fascinating in its own grim way.

Let's start with demand: In the 1990s, the medical community realized that chronic pain was a serious problem for lots of Americans. So doctors began prescribing far more legal opioid painkillers — Percocet, Vicodin, OxyContin — egged on by a marketing strategy from a pharmaceutical industry that saw a massive business opportunity. Those drugs, while effective in treating pain, can also be highly addictive. In 1999, deaths from painkiller abuse were at 2,000. By 2010, that number had risen to 16,600. By 2012, there were enough bottles of pills floating around for every adult in the country. The drugs made their way "to black markets, getting shared among friends and family, landing in the hands of teens who rummaged through parents' medicine cabinets, and so on," wrote Vox's German Lopez.

American drug enforcement began to clamp down on painkiller use, and placed stricter rules on prescriptions. But heroin comes from the same family of drugs, so deaths from its use began a steep increase — from 3,000 in 2010 to more than 10,500 in 2014 — as users were pushed away from their previous drug and toward an even more addictive alternative.

Another key player in this story was the failing economy, and the human suffering that came with it. The benefits of jobs and economic security go well beyond raw income: They also provide meaning, well-being, and social stability for individuals and communities. Meanwhile, joblessness is heavily implicated in all sorts of public health problems, from anxiety and depression to alcoholism and suicide. The opioid and heroin epidemic is at its worst in the rural areas, the Rust Belt, and Appalachian states, and among people without a college education — all groups that bear the brunt of stagnating wages, the flight of manufacturing, and the long absence of full employment.

What about the supply side?

That story begins with the Colombian drug cartels and their shipments into the U.S. through Florida. Stiff opposition from American law enforcement drove them to reroute through the southern border in the 1980s, requiring a business deal with the incipient drug cartels in Mexico. That partnership swelled the power and clout of the Mexican cartels themselves. So when America finally took out the heads of the Colombian cartels, the Mexican syndicates were ready to step into the void.

Joaquín Guzmán, nicknamed "El Chapo," and the head of the Sinaloa cartel, showed a particular capacity for adaptation and ingenuity that would've put the most flamboyant entrepreneur to shame. "He's trafficked cocaine from Colombia to Mexico in small private planes, in the luggage of airline passengers, and on the cartel's own 747s. Sinaloa has also moved cocaine on custom-built $1 million submarines," Theunis Bates wrote here at The Week. "[Guzmán has] built scores of tunnels under the border, some of which are air-conditioned and boast half-mile-long trolley lines. He's sent drugs through U.S. checkpoints in hidden car compartments, in cans of jalapeños, and in the bellies of frozen shark carcasses."

By 2014, the Sinaloa cartel controlled half of the $65 billion worth of illegal drug sales in America every year. And even with Guzmán's capture, other players like the hyper-violent Jalisco cartel are ready to step in.

What happened next will be familiar to any economist: The massive influx of supply drove prices into the ditch. The price of a gram of pure heroin fell from more than $2,000 in the early 1980s to under $200 by the late 2000s. Today, a standard bag of heroin bought on the street can go for as little as $10 in New York City and $5 in Baltimore. By comparison, an 80 milligram OxyContin pill will cost $60 to $100 on the black market.

Mass production also improved the product. A decade ago, a bag of heroin of 2 to 3 percent purity could be bought for $50 to $150. Today, 7 to 10 percent purity costs $10. That uptick in quality also means heroin can be smoked or snorted to get the desired high, not just injected, which dramatically widens the appeal to potential buyers. Ironically, it was when heroin was comparatively expensive that it was viewed as the drug for the poor, non-white underclass. The drug's incredible cheapness has coincided with its cultural rise up the income ladder.

One last under-appreciated factor was 1994's North American Free Trade Agreement (NAFTA), of all things. Critics usually focus on the deal's impact on American workers, but NAFTA also eviscerated local farmers in Mexico exposed to competition with U.S. agribusiness: By 2000, two million people had left their land. This created a massive pool of unemployed and desperate labor for the cartels to exploit, and drove a surge in the cross-border trading in which drugs could be hidden. Meanwhile, the farmers who remained on their land discovered that growing the poppies used to create opiates was one of the few routes to financial sustainability still available to them. That, too, drove heroin supply way up.

The collapse in heroin's price was more or less complete by the middle of the 1990s, laying the foundation for the drug's surge. Add to it the massive increase in painkiller use, along with the human and social destruction of our long rotting economy, and all the pieces were in place for the heroin epidemic to wreak havoc on the country.