Fixed #23844 -- Used topological sort for migration operation dependency resolution

Fixes https://github.com/django/django/pull/3525