Problem

This task consists of two parts, Part I and Part II. Note that you are not required to finish Part I in order to do Part II, and Part II is the opposite of Part I.

Alice and Bob are two frogs standing on a number line. Initially, Alice is at position $0$ and Bob is at position $N$. In one second, Alice jumps to the right by $A$ units while Bob jumps to the left by $B$ units.

During the jump, they are in mid-air. After several jumps, Bob and Alice may or may not meet at a point. Note that meeting in mid-air doesn’t count. If they’ll meet, denote that point to be position $P$.

Part I (50%)

Three integers $N, A$ and $B$ will be given. Your task is to determine the value of $P$ if they will meet with each other at some point. If they won’t meet, output $-1$.

Part II (50%)

Two integers $N$ and $P$ will be given. Your task is to find the smallest pair of $(A, B)$ where $A, B \ge 1$ such that after several jumps, Alice and Bob will meet at point $P$.

A pair $(A, B)$ is considered smallest if and only if both $A$ and $B$ are minimized. It can be shown that there is only $1$ unique solution.

It can be proven that there always exists at least one pair of $(A, B)$ such that after several jumps, Alice and Bob will meet.

Input

The first line contains an integer $T$, denoting which part the test case is.

If $T = 1$, the second line contains three integers $N, A$ and $B$.

If $T = 2$, the second line contains two integers $N$ and $P$.

Output

If $T=1$, if Alice and Bob will meet at some point $P$, output the value of $P$. Otherwise, output $-1$.

If $T=2$, output the smallest pair of $(A, B)$ such that after several jumps, Alice and Bob will meet with each other.

Subtasks

For all test cases, $2 \le N \le 10^9, 1 \le A, B \le 10^9, 0 < P < N$

Subtask Score Additional Constraints
$1$ $21$ $T = 1$
$A = B = 1$
$2$ $29$ $T = 1$
$3$ $27$ $T = 2$
$N \le 100$
$4$ $23$ $T = 2$

Sample Test Cases

Input Output
1
8 1 3
2

Note that this test case has $T=1$, which refers to part I of this problem.

After the first jump, Alice is located at $1$ while Bob is located at $5$.

After the second jump, Alice is located at $2$ while Bob is located at $2$.

They meet. Hence, the answer is $2$.

2
6 4
2 1

Note that this test case has $T=2$, which refers to part II of this problem

With $A=2$ and $B=1$, after the first jump, Alice and Bob are located at $2$ and $5$ respectively.

After the second jump, Alice and Bob are located at $4$ and $4$ respectively. They meet, so $(2, 1)$ is a pair of valid answer. It can also be proven that $(2, 1)$ is the smallest possible pair.

Click to copy.

Scoring: Per Subtask
Authored by s22f26
Appeared in 2026 Mini Comp 4