Phonomenal Reviews
InterviewTime limit2sMemory limit512 MB
Given a tree and a set of M marked nodes, find the minimum number of edges Jo must walk to visit every marked node, starting anywhere.
Problem
Jo is a blogger who reviews restaurants. Today she wants to visit every Vietnamese pho restaurant in the Waterloo area and decide which one is best.
Waterloo has restaurants, numbered to . Only of them are pho restaurants. Jo may start at any restaurant. Waterloo has roads, and each road connects exactly two restaurants. Using only these roads, Jo can travel from any restaurant to any other restaurant. Travelling along one road takes exactly 1 minute.
In computer science a road network with this structure is called a tree. Here are three examples of trees.

Every tree has this property. Between any two points there is exactly one path that repeats no road.
What is the least amount of time Jo has to spend travelling on roads to visit all of the pho restaurants?
Input
The first line contains two integers and ().
The second line contains distinct integers, the numbers of the pho restaurants.
Each of the next lines contains two integers. The and on line () describe a road between restaurant and restaurant .
Output
Print one line with one integer, the minimum number of minutes Jo has to spend travelling on roads in order to visit every pho restaurant.