The lesson many draw from the debacle in Afghanistan is no more nation-building. But you can’t ignore nation-building because military threats to the United States originate in one of two places—unstable or failed states and aggressive totalitarian societies. No democracy threatens the United States today. That fact alone suggests that the type of domestic regime matters greatly in the calculus of America’s national interests.

Going in or pulling out of Afghanistan does not end the matter. Had we not intervened after 9/11, an Al Qaeda-infested Taliban government in Afghanistan would have attacked the United States or its allies again. Now that we are leaving, ISIS-K and other terrorist groups are preparing to do just that.

So, should we stay indefinitely in places like Afghanistan and Iraq? The answer depends on whether the costs are worth the benefits compared to American interests in other parts of the world. We cannot afford to intervene in all failed or authoritarian countries that potentially threaten our interests. We thought we could in Afghanistan and Iraq, but as major authoritarian regimes emerged in Russia and China, that possibility diverted our attention from graver threats to democratic nations in Europe and Asia.

To keep our strategy in line with our resources and political will, we need to set priorities. We did that in the Cold War. The priority threats came from the Soviet Union in Europe and Asia, and we stationed troops abroad to counter that threat and to nurture less threatening, more democratic societies on the borders of freedom in those regions. Nation-building in this case worked beyond all expectations, but not without risks and costs. When we lost our sense of priority and pursued nation-building in peripheral regions, such as Vietnam, we lost our way.

After the Cold War, terrorism became the priority threat. A liberalizing Russia and globalizing China were not initially seen as major authoritarian adversaries. Radical Islam and an Islamic caliphate posed the new global ideological challenge, much as Marxism and communism had during the Cold War. The radical Islamist threat, however, was never as deadly as the Cold War threat. While ISIS quickly assembled a caliphate in 2013, it also just as quickly lost that caliphate. The persisting terrorist threat resembled neither a new ideological Cold War nor an existential military threat to America or its allies.

Nevertheless, after 9/11, out of both fear and hubris, the United States amplified the terrorist threat. It not only retaliated forcefully against the Taliban in Afghanistan; it invaded Iraq as well. Both invasions were remarkably successful. But what happened then? You guessed it: nation-building. New governments had to be put in place and a stable civil society constructed.

When instability persisted, the United States upped the military ante. President George W. Bush ordered a second surge in Iraq, and President Barack Obama followed with a look-alike surge in Afghanistan.

In 2011, Obama avenged 9/11 by killing Osama Bin Laden, the leader of Al Qaeda. Was that the time to declare victory and come home? Obama thought so, and American forces left Iraq entirely and drew down in Afghanistan. But what happened then? Instability spawned new threats. ISIS reemerged in Iraq, and the Taliban, Al Qaeda, and ISIS-K persisted in Afghanistan.

Withdrawing from Iraq did not end nation-building. And neither will withdrawal from Afghanistan. Minimal nation-building remains necessary to contain terrorism. But containing terrorism in these countries is no longer as important as dealing with the new authoritarian powers of Russia and China. These powers, to be sure, exacerbate terrorist threats in the Middle East (Russia’s support of Iran and Syria) and central Asia (China’s ambitions in Pakistan and Tajikistan), but they pose much more serious problems along the major borders of freedom in Europe (Ukraine and the Baltic states) and Asia (Hong Kong, Taiwan, and the Korean Peninsula).

What lessons can we draw from this experience?

First, recognize that threats are inevitably entangled with the nature of domestic regimes. It’s not an ideological matter of spreading democracy; it’s a practical matter of managing and incrementally reforming unstable and authoritarian regimes that generate the principal threats to American and allied security. Imagine what the world would be like today if, after World War II, the United States had dismissed the task of supporting nations in Europe and Asia threatened by instability and menacing authoritarian regimes on their borders.

Second, take a broad strategic view and set priorities. No nation is powerful enough to deter threats everywhere at once. The United States is far less dominant today than in 1945 or 1991. But the free world is far more dominant. Build on that base. Expect allies not only to do more but to lead more. The biggest mistake after 9/11 may have been to reject Europe’s offer to invoke Article V of the NATO Treaty.

On the borders of free countries in Europe and Asia, defend stable (South Korea) and struggling (Ukraine) democracies, recognizing that the loss of free governments in these countries would deal a more devastating blow to American and allied national security than any loss of freedom in Iraq or Afghanistan. Russian and Chinese interventions in Ukraine and Hong Kong are wake-up calls. Stand up to these threats by military (lethal weapons to Ukraine) and political (relentless public diplomacy on behalf of democracy advocates in Hong Kong) means. Pursue modest nation-building, largely through economic support by European and Asian allies.

In peripheral regions like Afghanistan, Iraq, Syria, Somalia, Yemen, and the like, contain terrorism but minimize nation-building. Had we reduced our presence in Afghanistan in 2002–03, after toppling the Taliban government, we would have left a local government that was far more capable of stabilizing the country, at least for a while, than the one that collapsed in 11 days this month. And we would have been in a better position to go back into Afghanistan later, if necessary, not having exhausted limited resources and domestic political will. In the future, keep small intelligence forces in these unstable countries, perhaps under European leadership (such as French leadership in North Africa). Watch for terrorist resurgence but intervene with large ground forces only if the local government is about to be overrun and then get back out quickly once the government is secured. Yes, it’s an imperfect strategy. But we did just the opposite in Afghanistan and Iraq, leaving no forces on the ground and watching instead of intervening as ISIS spread across Iraq and Syria and the Taliban blew away the Afghan government. And what did that get us?

Finally, accept the messiness and often meanness of our political system. Let the blame game play out. There will be partisan and petty debates about who lost Afghanistan and why we should never do nation-building again. But don’t get lost in the trees for the forest. Understand that national interests are a function of two calculations—how much power another entity has and what the nature of its domestic political leadership is. The two are tied at the hip. A powerful ideological ally like Japan poses no threat to the United States; a weak ideological adversary like ISIS may. And a strong ideological adversary like China deserves our principal attention.