Measurement of quarkonium production in proton-lead and proton-proton collisions at 5.02 TeV with the ATLAS detector