At the start of the 1960s Moscow had gained a new communist ally in Fidel Castro, but the US kept talking about invading Cuba. So Soviet Premier Nikita Khrushchev moved some nuclear-tipped missiles to Cuba to deter the US from attacking the island. However, from Cuba the Soviet missiles would be only five minutes away from their American targets. That caused panic in Washington.
Early in October 1962 the first Soviet SS-4 missiles arrived in Cuba, and American U-2 spy planes discovered them almost at once. President John F. Kennedy knew about them by October 16, but he did not go on television and warn the American public of the risk of nuclear war until October 22.
He then declared a naval blockade of Cuba, saying he would stop Soviet ships carrying more missiles from reaching the island by force if necessary. That would mean war, and probably nuclear war, but at least the blockade gave the Russians time to think before the shooting started.
The Soviet leaders were now desperately looking for a way out of the crisis they had created. After a few harrowing days a deal was done: the Soviet SS-4 missiles would be withdrawn from Cuba in return for a public promise by the United States not to invade Cuba.
The crisis was officially over by October 28, and everybody breathed a sigh of relief. It was the closest the world ever came to an all-out nuclear war.
Even so, they weren't really scared enough. They thought that a couple of hundred million people would die in a "nuclear exchange". At that time, nobody yet knew that detonating so many nuclear warheads would cause a "nuclear winter": the dust and smoke put into the stratosphere by firestorms in a thousand stricken cities would have blocked out the sunlight for a year or more and resulted in a worldwide famine.
What almost nobody knew until very recently is that the crisis did not really end on October 28.
A new book by Sergo Mikoyan, The Soviet Cuban Missile Crisis: Castro, Mikoyan, Kennedy, Khrushchev, and the Missiles of November, reveals that it continued all the way through November.
US intelligence was unaware that along with the SS-4s, the Soviet Union had also sent more than a hundred shorter-range "tactical" nuclear missiles to Cuba. They weren't mentioned in the Soviet-US agreement on withdrawing the SS-4s from Cuba, so technically Khrushchev had not promised to remove them.
Fidel Castro was in a rage about having been abandoned by his Soviet allies, so to mollify him, Khrushchev decided to let him keep the tactical missiles.
It was crazy: giving Fidel Castro a hundred nuclear weapons was a recipe for a new and even bigger crisis in a year or two. Khrushchev's deputy, Anastas Mikoyan, who was sent to Cuba to tell Castro the happy news, quickly realised that he must not have them.
The second half of the crisis, invisible to Americans, was Mikoyan's month-long struggle to pry Castro's fingers off the hundred tactical nuclear missiles.
In the end, he only succeeded by telling Castro that an unpublished (and in fact non-existent) law forbade the transfer of Soviet nuclear weapons to a foreign country. In December, they were finally crated up and sent home.
So it all ended happily, in one sense - but the whole world could have ended instead. As Robert McNamara, Kennedy's Defence Secretary in 1962, said 40 years later, "we were just plain lucky in October 1962 - and without that luck most of you would never have been born because the world would have been destroyed instantly or made unlivable in October 1962."
Then he said the bit that applies to us. "Something like that could happen today, tomorrow, next year. It WILL happen at some point. That is why we must abolish nuclear weapons as soon as possible."
They are still there, you know, and human beings still make mistakes.