I think now I got it. By putting one of the linear equation as negative. namely : m1g-T1=2m1a2.
For here I added two equation T1-T2+(m1g-T1)=Ma2+m1a1
or m1g-T2=Ma2+2m1a2
For this I can find T2=m1g-Ma2-2m1a2
then I can also find T3, 2T2-2T3=Ma2
Or T3= (2T2-Ma2)/2
which is cool thing since T2 is expressed purely in terms of m, M,a and g.
Using the last equation T2+T3-(M+m2)g=m2a2
And substitute value of T2 and T3. Hopping I didn't make any mistake I will get that
a2=1/m2(((3m1-5m1a2)/2) + (M+m2)g )
And I can also find a1. which is just twice a2