I think there is some confusion in your notation because it's really the nth term a_n = a + (n-1)d. a simply equals a. At some point in your calculations, the two were mixed up and treated as a single variable. Your working seems fine, just redo the calculations, making careful distinctions between a_n and a.

Another way to do this question is to simply add (md - md) = 0 to your equation for a_n