find an optimal matrix multiplication orrder