Measurement of the spin rotation parameter R in π−p and K−p elastic scattering at 40 GeV/c