Nasdaq, S&P 500 close with gains while Dow ends in negative territory