#P7731. 『PG1』[JDWOI-2] 猪猪大厦

『PG1』[JDWOI-2] 猪猪大厦

Description

piggy\texttt{piggy} built an extremely huge PIG\texttt{PIG} building, which can be seen as a plane. On this plane there are nn infinitely long escalators that are neither perpendicular nor parallel to the ground. They can be seen as linear functions, and all of them go to the right, i.e. in the positive direction of the xx axis.

At the intersection point of two escalators, you can transfer by paying 1 ZMB1 \ \texttt{ZMB}.

Someone wants to text piggy\texttt{piggy} to ask piggy\texttt{piggy} to come to find him.

However:

So he can only walk over to find piggy\texttt{piggy}.

He is currently at the point with xx-coordinate y1y_1 on the x1x_1-th escalator.

piggy\texttt{piggy} is at the point with xx-coordinate y2y_2 on the x2x_2-th escalator.

What is the minimum cost in ZMB\texttt{ZMB} for him to get there?

Input Format

The first line contains a positive integer nn.

The second line contains four integers x1,y1,x2,y2x_1, y_1, x_2, y_2.

The next nn lines: the ii-th line contains two numbers ki,bik_i, b_i, meaning the escalator is given by y=kix+biy = k_i x + b_i.

Output Format

Output the minimum cost in ZMB\texttt{ZMB}. If it is impossible to reach, output -1.

1
1 1 1 2
50 -30
0
2
1 2 2 2
1 0
2 0
-1
2
1 -1 2 1
1 0
2 0
1

Hint

This problem uses subtasks.

Subtask1(20pts)\sf Subtask1(20pts): 1n101 \le n \le 10.

Subtask2(30pts)\sf Subtask2(30pts): 1n10001 \le n \le 1000.

Subtask3(50pts)\sf Subtask3(50pts): 1x1,x2n1051 \le x_1, x_2 \le n \le 10^5, 103y1,y2,ki,bi103-10^3 \le y_1, y_2, k_i, b_i \le 10^3.

Escalators are numbered starting from 11.

Translated by ChatGPT 5