Define dp[u] as the maximum number of cities on any path from city to city . Initialize dp[1] = 1 (you start at city , so you have visited city) and dp[u] = -Infinity for all other cities. For each city in topological order, for each outgoing flight , update: dp[v] = max(dp[v], dp[u] + 1) After processing all cities, check dp[n].
If it is still , output "IMPOSSIBLE". Otherwise, dp[n] is the answer.