The history of the kingdom of Portugal and the Algarves, from the First Treaty of San Ildefonso and the beginning of the reign of Queen Maria I in 1777, to the end of the Liberal Wars in 1834, spans a complex historical period in which several important political and military events led to the end of the absolutist regime and to the installation of a constitutional monarchy in the country. In 1807, Napoleon ordered the invasion of Portugal and subsequently the royal family and its entire court migrated to Brazil, Maria I declaring the United Kingdom of Portugal, Brazil and the Algarves in 1815. This would be one of the causes for the declaration of Brazilian independence by Pedro I of Brazil in 1822, following a liberal revolution in Portugal. The liberal period was stormy and short as Miguel of Portugal (Pedro's brother) supported an absolutist revolution endeavoring to restore all power to the monarchy. Pedro eventually returned to Portugal and fought and defeated his brother in the Liberal Wars in which liberalism prevailed and Portugal became a constitutional monarchy. Queen Maria I The death of King Joseph in 1777 forced the accession of Princess Maria Francisca, his eldest daughter, to the throne of Portugal; she succeeded her father as the first Queen regnant of the 650-year-old country, which was still recovering from the 1755 Lisbon earthquake. Before becoming queen, Princess Maria and her husband, the Infante Pedro, lived on the sidelines of politics, but were clearly unsympathetic to her father's former Prime Minister, Sebastião José de Carvalho e Melo, the Marquis of Pombal, who had been the de facto ruler of the kingdom for the past 27 years. During her father's last few years, she had been the Marquis' fiercest detractor; once in power, she eagerly dismissed him and then exiled him to Pombal. Although the queen retained many of the Marquis' other ministers, she restored most of the privileges of the nobility and clergy, and released many of Pombal's political prisoners. The economy was reorganized and Pombaline monopolies were abandoned. However, international conditions favored the economic situation in Portugal as the balance of trade was positive, helped by wine exports and a decrease of British imports. The period was, while tainted by political instability, a time of cultural renovation, marked by the completion of the Palace at Queluz, the beginnings of the Ajuda Palace, the São Carlos Theatre,