Ab initio study of structural distortion and its influence on the magnetic properties of metallic dilute alloys