Derivatives and residual distribution of regularized M-estimators with application to adaptive tuning