Although the number of castles could vary between eight and twelve, Afonso IV would define them as twelve, and Sebastian I would finally fix them as seven. They supposedly represent the Moorish castles conquered by the Kingdom of Portugal during the Reconquista. Their origin is probably Castilian, but unlike Spanish castles, which usually have their gates coloured blue (hence opened), Portuguese castles were always depicted with gold gates (hence closed). As a matter of fact, Afonso III was the second son of King Afonso II of Portugal and thus was not expected to inherit the throne, which was destined to go to his elder brother King Sancho II of Portugal. As a second son, the coat of arms of Afonso III included both the arms of his father and the arms of his mother Urraca of Castile, thus the Castillan red border with golden castillan castles, around the Portuguese shield inherited from his father. An important element of Portuguese heraldry since the 15th century, the armillary sphere was many times used in Portuguese naval and colonial flags, mainly in Brazil. It was a navigation instrument used to calculate distances and represents the importance of Portugal during the Age of Discovery, as well as the vastness of its colonial empire when the First Republic was implemented. Although it is commonly regarded as a "republican" element, as opposed to the monarchist crown in the blue/white flag (see Flag of Portugal), its usage predates the republic by several centuries; it was the personal emblem of Manuel I. Some flags of the monarchic era, such as the flag of the United Kingdom of Portugal, Brazil and the Algarves, already depicted armillary spheres. The incorporation of the armillary sphere into the 1816 flag of the United Kingdom of Portugal, Brazil and Algarves is related to the adoption of the first flag of the Kingdom of Brazil, an armillary sphere on a blue background. The coat of arms sported different crowns during imperial rule of Portuguese and foreign crowns.