Equation of state of nuclear and neutron matter at third-order in perturbation theory from chiral effective field theory