Measurement of prompt and nonprompt J/psi production in pp and pPb collisions at root s(NN)=5.02 TeV