Alice has a rod. One day, she draws a path on a grid and puts the rod on it. The path begins at (0, 0) and continues to the right. The first and last segments are always horizontal, so there are always an odd number of segments. If we number the segments 1, 2, … , n, odd-numbered segments are all horizontal, while other segments (if any) are vertical. Initially, one endpoint B of the rod is located at (0, 0), and the other endpoint A is at (L, 0), where L is the length of the rod. The length of the first segment is at least L. When moving the rod, both endpoints A and B must be always on the path, though other parts may be outside. The rod is hard, so its length (i.e. distance between A and B) is always L.
Write a program to compute the minimum distance A must cover to reach the rightmost endpoint of the path.