Sky Walking
문제
Kenan drew a plan of the buildings and skywalks along one side of the main avenue of Baku. There are buildings numbered from to and skywalks numbered from to . The plan is drawn on a two-dimensional plane, where the buildings and skywalks are vertical and horizontal segments respectively.
The bottom of building is located at point and the building has height . Hence, it is a segment connecting the points and .
Skywalk has endpoints at buildings numbered and and has a positive -coordinate . Hence, it is a segment connecting the points and .
A skywalk and a building intersect if they share a common point. Hence, a skywalk intersects two buildings at its two endpoints, and may also intersect other buildings in between.
Kenan would like to find the length of the shortest path from the bottom of building to the bottom of building , assuming that one can only walk along the buildings and skywalks, or determine that no such path exists. Note that it is not allowed to walk on the ground, i.e. along the horizontal line with -coordinate .
One can walk from a skywalk into a building or vice versa at any intersection. If the endpoints of two skywalks are at the same point, one can walk from one skywalk to the other.
Your task is to help Kenan answer his question.