MADRID — President Barack Obama on Sunday urged Americans on all sides of the Black Lives Matter debate to “listen to each other” and not undercut the cause of reform through violence or careless talk.

“Whenever those of us who are concerned about failures of the criminal justice system attack police, you are doing a disservice to the cause,” Obama said during a brief news conference after meeting with Spain's prime minister here.

“First of all, any violence directed at police officers is a reprehensible crime and needs to be prosecuted,” Obama said. Beyond that, even rhetorically painting police with a broad brush or saying “things that are stupid or imprudent” risks harming the reform cause, he said.

The majority of protesters “who are doing the right thing, peacefully protesting” should not be held “responsible for everything that is uttered at a protest site,” he added.

Any protest movement will have “folks who say things that are stupid or imprudent or over-generalized or harsh,” Obama said. By contrast, he said, a thoughtful and respectful tone will help bring about real change.

He added that he thinks many law enforcement agencies, including the Dallas Police Department, are already taking seriously the problem of racial bias in criminal justice.

Obama's meeting with interim Prime Minister Mariano Rajoy was part of an abbreviated one-day visit here, from a planned two days. It was cut short by his decision to return to the U.S. early in the wake of a tumultuous week during which two black men were shot and killed by police officers in Louisiana and Minnesota and five police officers were killed by a gunman during a protest in Dallas.

On Sunday morning, Obama tried to comfort and calm from a distance. He called on police organizations to be respectful of the frustrations that black communities feel and said he hopes people don't dismiss Black Lives Matter concerns as political correctness.

“I'd like all sides to listen to each other,” he said.

“I wish I could stay longer,” Obama said as he stood alongside King Felipe VI at a formal welcome ceremony at the Palacio Real that had been moved up a day. “We've had a difficult week in the United States.”

Paying even a brief, one-day visit was important, Obama added, given the strong ties between the U.S. and Spain, a key NATO partner struggling with its own internal political turmoil.

“It is incredibly important for us to have such a strong partnership with the Spanish government and the Spanish people,” he said.

Obama was to have spent Sunday on a sightseeing tour through Seville, visiting sights such as the Alcazar and the Santa Maria de la Sede cathedral with the king.

Instead, he traveled directly to the Spanish capital for formal meetings with the head of state as well as Rajoy, who is struggling to form a government after a second inconclusive election.

The president continued with plans to visit a naval station in Rota, where he toured the guided-missile destroyer USS Ross, which is stationed there, and later addressed American troops. That long-planned stop was an especially important commitment for the commander in chief, officials said.

The U.S. naval presence there is a strategic one, part of the Aegis missile defense program and with a force capable of deploying to nearby regional hot spots, including northern Africa.

Obama was to return to Washington late Sunday night, ahead of a visit to Dallas on Tuesday to mourn the five killed in the Thursday attack.

michael.memoli@latimes.com