Help me in solving DAANEW2 problem

My issue

failed in hidden test case

My code

# cook your dish here

Learning course: Analysis and Design of Algorithms
Problem Link: Topological sort in Analysis and Design of Algorithms