Under construction

Still being built. Things may change

Modular Inverse

Interview

Time limit2sMemory limit1024 MB

Summary
Given a prime m and a number a with 1 at most a less than m, find b in [1, m) with a times b leaving remainder 1 modulo m.
Level

Easy3 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

The day before yesterday, Roma learned a new math operation: taking the remainder modulo mm, written amod⁡ma \operatorname{mod} m. For example, 5mod⁡3=25 \operatorname{mod} 3 = 2. Here amod⁡m=ba \operatorname{mod} m = b means a=m⋅k+ba = m \cdot k + b with 0≤b<m0 \le b < m, where all numbers are integers.

Yesterday Roma started multiplying numbers and noticed that sometimes (a⋅b)mod⁡m=1(a \cdot b) \operatorname{mod} m = 1, even when neither number is 1. He found this interesting and named it: aa and bb are called mutual inverses modulo mm.

This morning Roma looked at prime numbers mm. He proved that for every aa with 1≤a<m1 \le a < m, there is exactly one bb such that (a⋅b)mod⁡m=1(a \cdot b) \operatorname{mod} m = 1. However, he does not know how to find bb from aa and mm. Help him!

Input

A single line contains the numbers aa and mm (1≤a<m≤2⋅1091 \le a < m \le 2 \cdot 10^9). The number mm is prime.

Output

Print the number bb such that (a⋅b)mod⁡m=1(a \cdot b) \operatorname{mod} m = 1. It must also satisfy 1≤b<m1 \le b < m.

Examples2

  1. Example 1

    Input
    3 7
    
    Expected output
    5
    
  2. Example 2

    Input
    6 23
    
    Expected output
    4