This can be solved using the Chinese Remainder Theorem!
From here all I did was find all the divisors of each n, m1 and m2, that are co-prime to each other, because otherwise the CRT is unsolvable, and apply the CRT to all the pairs and returned the maximum.
I also made a sieve to find all divisors initially and then find M(n), this greatly boosted the speed.
My code took about 6mins, but I am very happy with my understanding.
Enter an integer (yourinput)
Code will output ∑M(n) for 1 ≤ n ≤ yourinput