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