Topological sorting exercise