Phase transitions at grain boundaries in the presence of impurities