Critical line of two-flavor QCD at finite isospin or baryon densities from imaginary chemical potentials