Pakistan's ex-PM Khan apologizes to avoid contempt charges