MADRID — It took the White House more than seven years to lock in Spain on President Obama’s foreign travel schedule. But events beyond Obama’s control have turned his first and only visit to Spain, the largest European country that had yet to welcome the president, into a rushed one.

Instead of spending two days sightseeing in southern Spain and tending to more official business in the capital of Madrid, the White House scrapped some of Obama’s events – including a staple of his foreign travels, a question-and-answer forum with young adults – and crammed the rest of his schedule into Sunday.

Deadly shootings last week of black men by police in Louisiana and Minnesota, followed by the sniper killings of five police officers in Dallas, led Obama to make the unusual choice to return to the White House late Sunday, a day earlier than originally planned.

Obama has been loath to tear up his schedule in response to previous acts of violence, saying repeatedly that altering his plans would be tantamount to giving in to terrorists. But terrorists didn’t strike in Baton Rouge, Louisiana, near St. Paul, Minnesota, or Dallas. The assaults followed June’s deadly shooting at an Orlando, Florida, nightclub and the rise in so-called lone-wolf terrorism, heightening fears about public safety.

Obama noted the “difficult week” as he made small talk Sunday with King Felipe VI after arriving at Spain’s Royal Palace for a meeting.

The king thanked Obama for visiting under the circumstances. The stop in Spain is the last leg of what is likely Obama’s final trip to Europe before he leaves office in January. He arrived late Saturday from Poland, where he attended a NATO summit.

The White House said the visit will highlight security cooperation between the trans-Atlantic allies, as well as political and economic ties.

Obama was also meeting with acting Prime Minister Mariano Rajoy before heading to Naval Station Rota, in the south. He planned to tour the USS Ross, one of four guided-missile destroyers based there, and address troops there before the flight home.

Spain has been gripped by a political stalemate for months, with Rajoy unable to garner the necessary support to form a new coalition government following a late-June election. It was the country’s second round of inconclusive balloting in the past year.

Rajoy’s party also won an election in December, but no other major party was willing to help him form a government.

Obama addressed the political situation in an interview Saturday with the El Pais newspaper, saying he hopes Spain’s next government will be just as committed to strong relations with the U.S. and Europe. He said the world needs Spain’s contributions to the campaign against the Islamic State group, to counterterrorism efforts that prevent attacks and to its fellow NATO members.

“The relationship between Spaniards and Americans goes back centuries,” he said. “We’re connected by the ties of family and culture, including millions of Americans who celebrate their Hispanic heritage. Spain is a strong NATO ally, we’re grateful for Spain’s many decades of hosting U.S. forces, and we’re major trading partners.”

“That’s why the United States is deeply committed to maintaining our relationship with a strong, unified Spain,” Obama said.

He said he has longed to return to Spain ever since he passed through while backpacking across Europe decades ago, during his 20s, a point he underscored just before his private meeting with the king. He said he could not have imagined that he’d return years later and be greeted by royalty.

“I wish I was staying longer,” Obama said Sunday. His wife, Michelle, and their daughters, Malia and Sasha, stopped in Spain in late June during the first lady’s three-country trip to promote education for adolescent girls in developing countries.