Alignment of the CMS muon system with cosmic-ray and beam-halo muons