Mandatory Play-doh
View as PDFOh no! Bob is at home, about to leave for school to attend his first period Calculus class, but he realizes that he is missing something very important. Bob and his class is on the unit Application of Integrals, and the teacher has requested all students to bring play-doh for some reason. However, Bob is an idiot, and forgot to bring play-doh with him beforehand. He does not want to get yelled at by the teacher.
Bob lives in a town with buildings, numbered from
to
and there are
unweighted bidirectional roads connecting them. The
road connects building
and
.
There are buildings
which sell play-doh. Bob is currently at home in building
and his school is at building
. He needs to visit at least one of the
buildings to buy play-doh so he can be ready for class.
Bob wants to arrive at school as fast as possible, so help him find the length of the shortest way to do so.
Note that it is guaranteed that there exists a way. Bob is allowed to pass through before buying the play-doh.
Input Specification
The first line of input will contain five space-seperated integers ,
,
,
,
The next line of input will contain space-seperated integers
, representing the buildings that stores that sell play-doh.
The next lines will contain two space-seperated integers,
and
.
Output Specification
Output a single integer, the shortest distance.
Constraints
Subtasks 1 [30%]
Subtasks 2 [30%]
Subtasks 3 [40%]
Sample Input 1
5 4 3 1 2
3 4 5
1 2
2 3
3 4
4 5
Output for Sample Input 1
3
Explanation of Output for Sample Input 1
Bob starts at his house which is at building . He passes by school at
, to head to
to buy some play-doh. He then returns to
, thus giving a total length of
.
Sample Input 2
5 5 1 1 2
4
1 2
2 3
3 4
4 5
5 1
Output for Sample Input 2
4
Comments