QOJ.ac

QOJ

実行時間制限: 2.0 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#10522. Rounding

統計

For an integer $x$, you can perform the following operation any number of times:

Choose a base $k$ such that $2 \le k \le m$, write $x$ in base $k$, and "round" it so that the last digit becomes 0. Formally, in one operation, you can choose an integer $k$ ($2 \le k \le m$) and update $x$ to $f(x, k)$, where:

$$f(x, k) = \begin{cases} \lfloor \frac{x}{k} \rfloor \cdot k & x \pmod k < \frac{k}{2} \\ \lceil \frac{x}{k} \rceil \cdot k & x \pmod k \ge \frac{k}{2} \end{cases}$$

Given $m$, find the minimum number of operations required to change $x$ to $y$ for multiple queries.

Input

Each test file contains only one test case. The first line contains two integers $q$ and $m$ ($1 \le q \le 10^5, 2 \le m \le 10^5$), representing the number of queries and the maximum available base, respectively. The next $q$ lines each contain two integers $x$ and $y$ ($0 \le x, y \le 10^5, x \neq y$), representing the initial value and the target value for each query.

Output

For each query, output a single integer representing the minimum number of operations required to change $x$ to $y$. If $x$ cannot be changed to $y$ through these operations, output "-1".

Examples

Input 1

5 10
4 10
3 11
11 3
5 0
1 72

Output 1

2
-1
5
2
23

Note

For the first query in the example, one optimal operation sequence is: $4 \xrightarrow{k=5} 5 \xrightarrow{k=10} 10$. For the second query in the example, it is impossible to reach 11 from 3, so the output is -1. For the third query in the example, one optimal operation sequence is: $11 \xrightarrow{k=8} 8 \xrightarrow{k=6} 6 \xrightarrow{k=5} 5 \xrightarrow{k=4} 4 \xrightarrow{k=3} 3$. For the fourth query in the example, one optimal operation sequence is: $5 \xrightarrow{k=4} 4 \xrightarrow{k=10} 0$. For the fifth query in the example, one optimal operation sequence is: $1 \xrightarrow{k=2} 2 \xrightarrow{k=3} 3 \dots \xrightarrow{k=10} 72$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.