First, I will give back the bimmer that was in an accident. It was in an accident so the Carfax will show it, your resale value will be very low. Also, it was in an accident, if it's not yours, walk away.
Second, I wouldn't buy an M5 except you can really afford the gas for it. Also, it is a beautiful car but is not cheap to maintain either.
If I would be in your place, I would look for a new 3 Series with a nice engine, whatever makes you fancy.
But if you are stuck between the two, than only because you had an accident, I will go with the M5.