Measurement of prompt and nonprompt J/psi production in pp and p Pb collisions at sqrt(s_NN)=5.02TeV