It should work like a dance for diplomats. Exact exits and exact entrances, all set around the danseur noble, in this case Mikhail Gorbachev.

It was, after all, the reason for the book with the baby-blue cover, the 59-page security master plan that police commanders carried with them throughout the summit.

That is the ideal, but when the whole thing gets on stage -- the motorcade, the media, the police, the crowds, the diplomats, the traffic -- things tend to change. Then the danseur breaks free and the ballet dissolves into a barn dance.

"The Russians are going to go wherever they want to go," said Capt. Darryl K. Harrison, commander of the special events branch of the District's police department and one of the principals upon whose shoulders security for the Soviets fell. "They decide, 'We're going to go there and we're leaving at this time,' and that's that."

For five weeks, Harrison, the police high command, the Secret Service and a host of other law enforcement agencies planned for Gorbachev's visit, a security event with few parallels. Veterans of state visits said only the pope commands as much manpower, and that is only because of the huge crowds he draws.

But despite their efforts to plan for the unexpected, a lesson learned after Gorbachev's 1987 visit, the huge security detail was in for surprises. Three times Gorbachev stepped out of his Zil limousine, creating what one sergeant on the motorcade detail called "controlled pandemonium."

Harrison and Police Chief Isaac Fulwood Jr. had discussed this possibility Wednesday, when Gorbachev arrived, and the police came up with a last-minute plan. Motorcade One, as Gorbachev's security detail was called, was shadowed by twin scooter police patrols traveling along parallel streets. As soon as police broadcast, "He's out," the scooters swooped to the scene to control the crowd.

"That's something we're very proud of because it's a major change in the middle of the process," said Harrison, who was interviewed several times during Gorbachev's stay. "It's something that wasn't practiced, and even the Russians are complimenting us."

Gorbachev's brushes with the crowd are the most harrowing for those charged with protecting him. But ultimately it is a risk he takes himself, and that decision, the police said, absolves them if something were to occur. What police worried about was adjusting their transportation plan to fit the Soviet leader's whim.

The most confusing moment occurred Friday night, when Gorbachev was about to leave the Soviet Embassy on 16th Street NW between L and M streets. He was running late by about half an hour. Just as police had cleared his route, officials heard that Soviet Foreign Minister Eduard Shevardnadze's motorcade was heading for the embassy.

That caught everyone by surprise and resulted in what Harrison called a "no-no." Gorbachev's motorcade was held back, a highly unusual move, while Shevardnadze's entourage pulled in.

Harrison and Lt. Jose Acosta, the department's point man at the embassy, quickly coordinated a new departure. By then, the crowds at the intersection of Connecticut Avenue and L Street were so large that Sgts. James Hunt and Russell Brigham, the route leaders for the motorcade, ordered a last-minute change.

The motorcade veered left on L Street and traveled east as officers frantically fought to clear traffic. And then, when Harrison and Acosta thought they were clear, they heard that Gorbachev had stopped on 15th Street, near McPherson Square.

The Soviets set the stage for this even before Gorbachev arrived, when tempers weren't even taut. Several members of the official welcoming party left the new Soviet Embassy compound in Northwest without their escort. News broke over the police radio, and the hunt was on.

Within 15 minutes, the radio crackled again. The quarry was found and the party was on its way to Andrews Air Force Base, where Gorbachev arrived almost two hours later.

On another occasion, one of the motorcades carrying Soviet delegates inexplicably divided en route. Cars went in two directions, and the tail of the police escort suddenly found itself leading one of the stray groups. The same thing happened in 1987, and Harrison said the police had practiced this maneuver.

As a matter of policy, the police department does not provide manpower figures, but it took at least 500 District officers to shepherd the Soviets and their seven motorcades round-the-clock. Each police district supplied the same number of officers, who worked regular eight-hour shifts. Police cadets were used to direct traffic, and Fulwood said there was a sufficient number of officers detailed to non-summit-related duty.

That army was supplemented by another battalion of agents from the Secret Service and other law enforcement agencies. The Soviets brought their own security contingent as part of their delegation of about 250 people. Harrison's branch, which falls under the department's special operations division, protects dignitaries while they are in the District, including the U.S. president and the vice president.

Veteran police officials who have weathered many state visits tended to dismiss Gorbachev's visit as more of the same, only bigger. But in their faces you could see tension every time the police radio broadcast what became a familiar call: "All units: We have a departure at this time. Motorcade One. Restrict transmission."

"We're always concerned when we get a visiting head of state because of the international implications if anything happens," said Lt. Don R. Pope, who spent the summit in the department command center, a bus equipped with communications gear parked near the White House.

"One of the things that makes us good is that we handle this kind of details on a regular basis, and very seldom do we have anything go astray," Pope said.

That is not to say things don't get tense. The natural enemies for the police are, as Harrison put it, "the media and every official who thinks he's somebody."

Two television networks violated police regulations by parking vans along the motorcade route on Friday. Gorbachev's ventures into the crowds brought the police and the media face-to-face, and a photographer and a television cameraman accused officers of roughing them up.

Gary Kieffer, a stringer for the Los Angeles Times, said he was thrown to the ground by a police captain on Thursday. Kieffer said he pulled the tendons on his left hand and was unable to work the rest of the summit.

Shelly Fielman, a cameraman with NBC, said he was within 10 feet of Gorbachev on Friday when he was grabbed from behind by a police officer. He said he ended up on the ground, after being shoved several times by three police officers. One of the officers, he said, clubbed him on the forearm.

Lt. Reginald Smith, a police spokesman, said he was unaware of the two incidents and declined to comment.