Nucleon and pion distribution functions in the valence region