CSES - Aalto Competitive Programming 2024 - wk11 - Homework - Finding inverse
  • Time limit: 1.00 s
  • Memory limit: 512 MB

You are given two numbers: aa and MM. Your task is to find xx such that ax1(modM)a\cdot x \equiv 1 \pmod{M} and 0<x<M0 < x < M or report that such number doesn't exist.

Input

The only line consists of the numbers aa and MM.

Output

The output consists of one integer: xx if it exists or -1 if such xx doesn't exist.

Constraints

  • 0a<m1090 \leq a < m \leq 10^9

Example

Input:

3 5

Output:

2

Explanation: 32=61(mod5)3 \cdot 2 = 6 \equiv 1 \pmod{5}

Input:

4 6

Output:

-1

Explanation: 4x4\cdot x is either 0, 2 or 4 modulo 6 for any xx.