HELSINKI (Reuters) - Finland’s Social Democrats (SDP) embarked on Monday on the complex task of forming a governing coalition, after beating a nationalist, anti-immigration party by a hairsbreadth in the most fragmented election in the country’s history.

The SDP, which finished first in Sunday’s ballot with 17.7 percent, could team up with two smaller left-wing parties, its leader Antti Rinne said.

“At first sight they feel like the most natural partners,” he told private news outlet Lannen Media, referring to the Greens and the Left Alliance, which scored 11.5 percent and 8.2 percent respectively.

But coalition talks are expected to take weeks after the first Finnish election in which no party won 20 percent, leaving a polarised parliament that reflects deep social divisions over immigration and the environment, and how to reform a creaking welfare system deeply rooted in Nordic social traditions.

Rinne, a 56-year-old former trade union leader tasked by convention with forming a government as the head of the biggest party, would have to add at least a fourth party to give him a parliamentary majority.

But he is at odds over the future shape of public services with the centrist and center-right groups that he might try to ally with, and he has ruled out any cooperation with the nationalist, eurosceptic Finns Party, which won 17.5 percent of the vote.

The leftists want to preserve the welfare system through tax hikes while the center-right wants to see it streamlined because of rising costs linked to a rapidly ageing population.

“The rising inequality has to be turned around. We need to invest in education and equality in the labor market,” Rinne said.

Meanwhile, chiming with a groundswell of support for far-right parties across Europe, the message from the Finns Party has resonated with voters who believe the nation has gone too far in addressing issues such as climate change and migration at its own expense.

Its leader Jussi Halla-aho, fined by the Supreme Court in 2012 for blog comments linking Islam to pedophilia and Somalis to theft, won over 30,000 constituency votes on Sunday, more than any other parliamentary candidate.

The centre-right government of Prime Minister Juha Sipila resigned last month, saying it could not deliver on a long-delayed healthcare reform widely seen as crucial to securing the long-term viability of government finances.

Until a new government is chosen Sipila, whose Centre Party won 13.8 percent on Sunday, will remain as head of a caretaker cabinet.

Finland’s largest business daily Kauppalehti said the Social Democrats, the centre-right National Coalition - which won 17.0 percent - and the Greens were most likely to form the core of the next government.

But Rinne faced “a mountain-sized challenge” to form a workable parliamentary majority, it wrote.