#P8220. Minimum Time to Finish the Race

    ID: 21399 Type: Default 1000ms 256MiB

Minimum Time to Finish the Race

Minimum Time to Finish the Race

A contestant starts at \((1,1)\) in the first quadrant of the coordinate plane and must reach \((n,n)\). The contestant may move one unit at a time in one of the four cardinal directions: up, down, left, or right; however, at all times the contestant must remain in the region where \(x>0, y>0\).

Initially every point is of type \(A\) and takes \(1\) second to traverse. Then the organizers choose a triple \((a,b,c)\) and change the type of each point \((x,y)\) satisfying \[ a \le x \le b \quad \text{and} \quad y \le c \] to type \(B\) (each such point takes \(2\) seconds to traverse). Note that both the starting point and the finishing point are included in the calculation.

The contestant, Kid, wants to know the minimum time (in seconds) required to go from \((1,1)\) to \((n,n)\).

Important: The data constraints are crucial in designing an efficient solution.

inputFormat

The input consists of a single line with four integers:

  • \(n\) --- the coordinates of the destination point \((n,n)\),
  • \(a\), \(b\), \(c\) --- the parameters defining the B-type region.

You may assume that \(1 \le n \le 1000\) and that \(1 \le a \le b \le n\) and \(1 \le c \le n\).

outputFormat

Output the minimum number of seconds required to reach \((n,n)\) from \((1,1)\) following the optimal route.

sample

5 1 100 1
10