Two weeks before securing his latest electoral victory, Viktor Orbán, Hungary’s prime minister, travelled to the northern Serbian town of Subotica to meet his close friend, Aleksandar Vučić, the president of Serbia. Outside the region, few people pay much attention to the bond between these two men. Yet overlooking it would be hazardous. A close look at this duo can reveal a lot about the nexus, in today’s Europe, between political engineering, nationalism, and how demagogues can thrive.

Orbán and Vučić like each other because they have much in common, although one has made the EU an enemy while being part of it, while the other remains outside and aspires to join the club. They’re roughly of the same generation – one that entered politics in the turbulent era of the crumbling of the communist bloc. They both pander to nationalist sentiment, while toeing a fine line trying to preserve a functional relationship with important players within the EU – not least in Germany. And they are strongmen, with little interest indeed for checks and balances, liberal democracy or free media.

But what is most fascinating – and also very telling of this particularly inchoate phase of European politics – is how Orbán and Vučić have managed to reinvent themselves spectacularly over the years. Their recipe for political survival and entrenchment lies in being perfect chameleons. They may infuse state-controlled television with constant ideology, but they hardly hold deep beliefs. Slogans are to them just a dress code that can be replaced, whenever convenient. They draw their strength from having a good nose for where the wind of public opinion may blow – and how that can best be exploited.

Look at their careers. Orbán went from a late-1980s poster-child of democratic transition, to a trailblazer of Europe’s current national-populist nightmares. Vučić was once a staunch admirer of Serbia’s blood-drenched warlords in the 1990s Balkans, and at one point he joined Slobodan Milošević’s propaganda outfit as information minister. But by the 2010s, he had mutated into a seemingly reasonable, eager pro-European. He had well understood that dreams of a “Greater Serbia” had collapsed in national exhaustion and defeat.

Just as Orbán had detected that Brussels’ attractiveness was waning and decided it was time to cast his political net elsewhere, Vučić saw opportunity in self-transformation. He tacked on to a formally pro-European course, while Orbán opted to go the other way. The directions seem opposite, but the motivation is the same: to secure power. To be sure, Machiavellian manoeuvring is hardly rare currency in politics. Orbán and Vučić are just good at it. But what matters here is the impact these shifts may have on Europe’s future – and whether there’s any possibility of further and perhaps altogether different shifts.

After Orbán and Vučić met in Subotica, the talk in Belgrade was all about passions flaring up again over Kosovo

These two men matter. Orbán’s Hungary is a magnet for the far right elsewhere on the continent. Serbia holds the key to the stability of the Balkans – a region that forms Europe’s strategic, vulnerable underbelly, and which Winston Churchill once described as “producing more history than it can consume”.

In his book on Europe, Postwar, the historian Tony Judt played down the role of intellectual dissident groups in consolidating the 1989 revolutions of central Europe. He pointed, rather, to how “party functionaries and bureaucrats transformed themselves in a matter of months from nomenklatura yes-men to glib practitioners of pluralist party politics”.

“Survival,” Judt wrote, “depended on recalibrating one’s allegiances.” “Market economy”, “democratisation” and “civil society” (or “Europe”, which encompassed all three) became the buzzwords. This is something Orbán himself experienced. In Yugoslavia, unlike in Hungary, things were different, because various populations were so intermingled. Nationalism became Milošević’s way of securing a hold on power. That’s a story Vučić became part of.

So what about today? I happened to be in Belgrade shortly after Orbán and Vučić met in Subotica. The talk among politicians and civil society activists was all about passions flaring up again over Kosovo. Speculation ran high as to whether Vučić might choose to play on tensions in the region to recast himself as a territory-grabbing ultra-nationalist. It could be his way of compensating for a loss of personal legitimacy if EU membership prospects don’t materialise.

Interestingly, there were also comments about Orbán’s interest in the Hungarian minority in Vojvodina (many of whom voted last Sunday, as dual citizens), where Subotica is located. The most pessimistic take I heard was this: that Orbán and Vučić will next engage in a game of revisiting borders, playing on Hungarian grievances about the 1920 treaty of Trianon and Serbian obsessions over Kosovo. For all the talk about democratic backsliding, ethnic and territorial questions may be the next looming crisis for Europe, as far-fetched as it may seem.

The EU has only one tool to prevent such worst-case scenarios, which is to stick together and defend its principles by using its soft power of pressures and incitements. It needs to keep opportunist demagogues from giving way to their darkest temptations – and to act now. Remember, these are chameleons, they adapt to circumstance. Just as they changed their colour before, they might just change it again. The question is how.

• Natalie Nougayrède is a Guardian columnist