Multiparticle azimuthal correlations in p-Pb and Pb-Pb collisions at the CERN Large Hadron Collider