 Imperial invasions were led by more or less all medieval Emperors, the most notable episodes being the end of the Investiture controversy by the pilgrimage of Henry IV, Holy Roman Emperor at Canossa in 1077 and the no less than five major invasions staged by Frederick Barbarossa against the Lombard League, culminating in the sack of Milan in 1162, after which every building in the city was demolished, except the churches.The lasting conflict led to the emergence of the Guelph and the Ghibelline parties in northern Italy, supporting respectively the Pope (and the independent cities) and the Emperor, though siding with a party was often dictated by other political considerations (more or less each city has belonged to both parties).In May 1176, the Lombard League, led by a revived Milan, defeated the Emperor Frederick Barbarossa at Legnano.