Not able tosove this q

I am getting intuition like may be we ahve to apply topological sort. but not getting idea other than that.
Just explain it’ s solution(approach). not a solution code.