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