Trapping and release of impurities in TiN : A first-principles study