Measurement of prompt and nonprompt J / ψ production in p p and p Pb collisions at √sNN=5.02TeV