MADRID, OCT. 26 -- Soviet President Mikhail Gorbachev, leaving behind a land afflicted by food shortages and nationalist strife, opened a five-day trip to Western Europe today by seeking economic support from Spain and advice on how it shed a totalitarian legacy to become a prosperous democracy.

Gorbachev, making the first trip to Spain by a Soviet leader, met with King Juan Carlos and held more than two hours of talks with Prime Minister Felipe Gonzalez. Later, in a speech to the Spanish parliament, Gorbachev made a strong appeal for economic help from abroad to save the Soviet Union from collapse.

"It is no coincidence that I am here at the most tense period of our difficult transition," Gorbachev said. "In these conditions we are counting on the support of the world, including Spain."

He said the wrenching economic changes and ethnic challenges to Moscow are viewed by many people "as the advent of chaos, as a catastrophe that cannot be corrected. But in essence it is the birth of a powerful new body and that birth is inevitably painful."

Gorbachev contended that moderate voices are now ascendant, saying that "in the last few weeks, euphoria and extremism have been losing credibility. Common sense and responsibility are gaining momentum, and there is growing understanding that we can stabilize the situation and make progress only by consolidating all our forces."

After further discussions Saturday, Gorbachev and Gonzalez are expected to sign a declaration of friendship between their countries and several economic accords designed to boost trade and investment, including credits for Spanish goods worth up to $1.5 billion.

Similar pacts will be signed Monday in France, in what Soviet officials described as a process of laying foundations for a new era of friendship and cooperation with the West, including the United States.

Gorbachev has taken pains to stress that his vision of building a "common European home" includes North America. In an interview published today in the Madrid daily El Pais, Gorbachev said the Persian Gulf crisis "has not perturbed but strengthened Soviet-U.S. cooperation."

"This indicates we have found the key to common efforts to create a safer and more just world order," Gorbachev said in the interview. "We believe we are at the start of a long and stable period of normal, strong and equal relations" with the United States.

A Spanish spokesman said much of the first round of talks was taken up with a discussion of how Spain managed to heal the painful divisions left by the brutal civil war of 1936-39 and nearly four decades of Gen. Francisco Franco's dictatorship. In his speech at the parliament, Gorbachev alluded to the lessons he hopes his country can draw from Spain's experience.

"We are living through an unprecedented period in liberating our society and achieving a profound transformation of a multinational country," Gorbachev said. "There is much to learn from the manner in which your country overcame confrontations and antagonisms suffered from the 1930s. Spaniards summoned the moral strength to set aside irreconcilable discord, turn a tragic page of history and move ahead on the path to radical change."

Gorbachev praised Spain's political leadership for "dismantling authoritarian structures in a short space of time, without much loss of blood, and making an orderly transition to democratic forms of government and public life."

But in his talks with Gonzalez, the Soviet leader heard a sobering message that the path to a flourishing democracy and thriving economy is not painless. According to his spokesman, Gonzalez told the Soviet leader that it took Spain 10 years to establish a climate of stability and confidence that finally attracted a rush of new investors. But for a long period after Franco's death in 1975, Spain required substantial economic help from its European allies.

The Soviet Union established relations with Spain in 1977 after the long period of estrangement under Franco.

Trade between Spain and the Soviet Union currently amounts to about $2 billion a year, 70 percent of it in Soviet oil exports. Besides receiving two honorary degrees, Gorbachev will meet with leading businessmen in Madrid and Barcelona to urge them to embark on fresh joint ventures in the Soviet Union.