You are given a rooted tree consisting of N vertices numbered from 1 to N, where vertex 1 is the root. Some vertices contain tasks that must be completed. Whenever you visit a vertex containing a task, the task is completed instantly.
You start at the root (vertex 1) and must finish all tasks. After completing every task, you must end your journey at a given vertex T.
Moving along any edge takes 1 second.
Find the minimum time required.
Print a single integer — the minimum time required.
Explanation
The tasks are located at vertices 4 and 6.
One optimal route is:
1 → 2 → 4 → 2 → 1 → 3 → 6 → 3 → 7
All tasks are completed, and the journey ends at vertex 7.
The total time taken is 8 seconds.