Necessary optimality conditions in systems with lags and phase constraints