#P10631. Golf

    ID: 12657 Type: Default 1000ms 256MiB

Golf

Golf

Translated from JOI Open 2017 T3 "Golf".

In the first quadrant there are \(N\) rectangular obstacles. Each rectangle \(i\) \( (1\le i\le N)\) has two pairs of parallel sides with the \(x\) and \(y\) axes. The rectangle \(i\) has its bottom‐left corner at \((A_i,C_i)\) and its top‐right corner at \((B_i, D_i)\). Any two obstacles (including their boundaries) do not intersect.

JOI must hit a golf ball from \((S,T)\) to \((U,V)\). It is guaranteed that \((S,T)\) and \((U,V)\) are distinct and neither lies inside or on the boundary of any obstacle.

JOI is only allowed to hit the ball in directions parallel to the \(x\) or \(y\) axis (and he may move with the ball). The ball may travel along the boundaries of obstacles but cannot enter the interior. When the ball collides with an obstacle, it stops (JOI may then hit it again in another direction).

Determine the minimum number of strokes required to reach \((U,V)\).

inputFormat

The first line contains five integers: \(N, S, T, U, V\).

Each of the next \(N\) lines contains four integers: \(A_i, B_i, C_i, D_i\) that describe an obstacle.

outputFormat

Output a single integer representing the minimum number of strokes required to hit the ball from \((S,T)\) to \((U,V)\).

sample

0 1 3 4 3
1

</p>