Study of the production of Lambda0b and B0 hadrons in pp collisions and first measurement of the Lambda0b to J/psi p K branching fraction