Although Poland-Lithuania was unaffected by the Thirty Years' War (1618–1648), the following two decades subjected the nation to one of its worst trials ever. This colorful but ruinous interval, the stuff of legend and popular historical novels of Nobel laureate Henryk Sienkiewicz, became known as "potop", or the Deluge, for the magnitude and suddenness of its hardships. The emergency began when the Ukrainian Cossacks rose in revolt and declared an independent state based in the vicinity of Kiev, allied with the Crimean Tatars and the Ottoman Empire. Their leader Bohdan Khmelnytsky defeated Polish armies in 1648 and 1652, and after the Cossacks concluded the Treaty of Pereyaslav with Russia in 1654, Tsar Alexis overran the entire eastern part of the Commonwealth (Ukraine) to Lwów (Lviv). Taking advantage of Poland's preoccupation in the east and weakness, Charles X Gustav of Sweden intervened. Most of the Polish nobility along with the Polish vassal Frederick William of Brandenburg-Prussia agreed to recognize him as king after he promised to drive out the Russians. However, the Swedish troops embarked on an orgy of looting and destruction, which caused the Polish populace to rise up in revolt. The Swedes overran the remainder of Poland except for Lviv and Danzig (Gdańsk). Poland-Lithuania rallied to recover most of its losses from the Swedes. In exchange for breaking the alliance with Sweden, Frederick William, the ruler of Ducal Prussia, was released from his vassalage and became a "de facto" independent sovereign, while much of the Polish Protestant nobility went over to the side of the Swedes. Under Hetman Stefan Czarniecki, the Poles and Lithuanians had driven the Swedes from the Commonwealth's territory by 1657. The armies of Frederick William intervened and were also defeated. Frederick William's rule over East Prussia was recognized, although Poland retained the right of succession until 1773.