Max Utility Function and Finding associated demand curve
From your formula for $x$ when $y=0$, you should be able to find $U$ in terms of $M$ and $p_x$ when $y=0$.  Similarly, $U$ in terms of $M$ and $p_y$ when $x=0$.  The key then is to find the critical price ratio at which, to maximise $U$, the switch needs to occur from $y=0$ to $x=0$.

Can you take it from there?