The campus has $$$m$$$ rooms numbered from $$$0$$$ to $$$m - 1$$$. Also the $$$x$$$-mouse lives in the campus. The $$$x$$$-mouse is not just a mouse: each second $$$x$$$-mouse moves from room $$$i$$$ to the room $$$i \cdot x \mod{m}$$$ (in fact, it teleports from one room to another since it doesn't visit any intermediate room). Starting position of the $$$x$$$-mouse is unknown.You are responsible to catch the $$$x$$$-mouse in the campus, so you are guessing about minimum possible number of traps (one trap in one room) you need to place. You are sure that if the $$$x$$$-mouse enters a trapped room, it immediately gets caught.And the only observation you made is $$$\text{GCD} (x, m) = 1$$$.

The only line contains two integers $$$m$$$ and $$$x$$$ ($$$2 \le m \le 10^{14}$$$, $$$1 \le x < m$$$, $$$\text{GCD} (x, m) = 1$$$) — the number of rooms and the parameter of $$$x$$$-mouse.

Print the only integer — minimum number of traps you need to install to catch the $$$x$$$-mouse.

NoteIn the first example you can, for example, put traps in rooms $$$0$$$, $$$2$$$, $$$3$$$. If the $$$x$$$-mouse starts in one of this rooms it will be caught immediately. If $$$x$$$-mouse starts in the $$$1$$$-st rooms then it will move to the room $$$3$$$, where it will be caught.In the second example you can put one trap in room $$$0$$$ and one trap in any other room since $$$x$$$-mouse will visit all rooms $$$1..m-1$$$ if it will start in any of these rooms.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    public List<long> Factorize(long x) {
        var res = new List<long>();
        for (long i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                res.Add(i);
                while (x % i == 0) x /= i;
            }
        }
        if (x != 1) res.Add(x);
        return res;
    }

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 0;
        // x^phi(m) = 1 (mod m)
        var primes = Factorize(m);
        var len = primes.Count;
        for (int i = 0; i < len; i++) {
            primes.AddRange(Factorize(primes[i] - 1));
        }
        primes.Sort();
        primes = primes.Distinct().ToList();

        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            var phi = Phi(m2);
            long minPower = phi;
            foreach (var p in primes) {
                if (p > minPower) break;
                if (minPower % p == 0) {
                    int cnt = 0;
                    while (minPower % p == 0) {
                        minPower /= p;
                        cnt++;
                    }
                    for (int i = 0; i < cnt; i++) {
                        if (BigInteger.ModPow(x, minPower, m2) == 1) break;
                        minPower *= p;
                    }
                }
            }

            ans += phi / minPower;
        }
        return ans;
    }

    public static long ModPow(long x, long n, long mod) {
        long t = x, ret = 1;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret = (long)(((decimal)t * ret) % mod);
            }
            t = (long)((decimal)t * t % mod);
            n >>= 1;
        }
        return ret;
    }

    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    Dictionary<long, List<long>> cache = new Dictionary<long, List<long>>();

    public List<long> Divisors(PrimeSet primes, long x) {
        //if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long> { 1 };
        foreach (var p in primes) {
            if (p * p > x) break;
            int last = 0;
            while (x % p == 0) {
                int count = ret.Count;
                for (int i = last; i < count; i++) {
                    ret.Add(ret[i] * p);
                }
                last = count;
                x /= p;
            }
        }
        if (x != 1) {
            int count = ret.Count;
            for (int i = 0; i < count; i++) {
                ret.Add(ret[i] * x);
            }
        }
        cache[x] = ret;
        return ret;
    }

    public List<long> Divisors(long x) {
        if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        cache[x] = ret;
        return ret;
    }

}

public class PrimeSet : List<int> {
    private readonly bool[] isPrime;

    public PrimeSet(int size)
        : base() {
        isPrime = Enumerable.Repeat(true, size).ToArray();
        Add(2);
        for (int i = 3; i <= size; i += 2) {
            if (!isPrime[i >> 1]) {
                continue;
            }

            Add(i);
            for (long j = (long)i * i; j <= size; j += i + i) {
                isPrime[j >> 1] = false;
            }
        }
    }

    public bool IsPrime(long x) {
        if (x == 2) {
            return true;
        }

        if (x <= 1 || x % 2 == 0) {
            return false;
        }

        return isPrime[x >> 1];
    }
}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    public List<long> Factorize(long x) {
        var res = new List<long>();
        for (long i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                res.Add(i);
                while (x % i == 0) x /= i;
            }
        }
        if (x != 1) res.Add(x);
        return res;
    }

    /// <returns>
    /// return min d where a^d = 1 (mod m)
    /// </returns>
    private long MultiplicativeOrder(IEnumerable<long> primes, long a, long m) {
        var phi = Phi(primes, m);
        var res = phi;
        foreach (var p in primes) {
            if (p > phi) break;
            while (res % p == 0 && BigInteger.ModPow(a, res / p, m) == 1) {
                res /= p;
                phi /= p;
            }
            while (phi % p == 0) phi /= p;
        }
        if (phi > 1 && BigInteger.ModPow(a, res / phi, m) == 1) {
            res /= phi;
        }
        return res;
    }

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 0;
        // x^phi(m) = 1 (mod m)
        var primes = Factorize(m);
        var len = primes.Count;
        for (int i = 0; i < len; i++) {
            primes.AddRange(Factorize(primes[i] - 1));
        }
        primes.Sort();
        primes = primes.Distinct().ToList();

        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            var phi = Phi(m2);
            ans += phi / MultiplicativeOrder(primes, x, m2);
        }
        return ans;
    }

    public static long ModPow(long x, long n, long mod) {
        long t = x, ret = 1;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret = (long)(((decimal)t * ret) % mod);
            }
            t = (long)((decimal)t * t % mod);
            n >>= 1;
        }
        return ret;
    }

    public static long Phi(IEnumerable<long> primes, long n) {
        long phi = n;

        foreach (var p in primes) {
            if (p * p > n) break;
            if (n % p == 0) {
                while (n % p == 0) {
                    n /= p;
                }
                phi = phi / p * (p - 1);
            }
        }

        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }
    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    Dictionary<long, List<long>> cache = new Dictionary<long, List<long>>();

    public List<long> Divisors(PrimeSet primes, long x) {
        //if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long> { 1 };
        foreach (var p in primes) {
            if (p * p > x) break;
            int last = 0;
            while (x % p == 0) {
                int count = ret.Count;
                for (int i = last; i < count; i++) {
                    ret.Add(ret[i] * p);
                }
                last = count;
                x /= p;
            }
        }
        if (x != 1) {
            int count = ret.Count;
            for (int i = 0; i < count; i++) {
                ret.Add(ret[i] * x);
            }
        }
        cache[x] = ret;
        return ret;
    }

    public List<long> Divisors(long x) {
        if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        cache[x] = ret;
        return ret;
    }

}

public class PrimeSet : List<int> {
    private readonly bool[] isPrime;

    public PrimeSet(int size)
        : base() {
        isPrime = Enumerable.Repeat(true, size).ToArray();
        Add(2);
        for (int i = 3; i <= size; i += 2) {
            if (!isPrime[i >> 1]) {
                continue;
            }

            Add(i);
            for (long j = (long)i * i; j <= size; j += i + i) {
                isPrime[j >> 1] = false;
            }
        }
    }

    public bool IsPrime(long x) {
        if (x == 2) {
            return true;
        }

        if (x <= 1 || x % 2 == 0) {
            return false;
        }

        return isPrime[x >> 1];
    }
}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;
using System.Numerics;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    public List<long> Factorize(long x) {
        var res = new List<long>();
        for (long i = 2; i * i <= x; i++) {
            if (x % i == 0) {
                res.Add(i);
                while (x % i == 0) x /= i;
            }
        }
        if (x != 1) res.Add(x);
        return res;
    }

    /// <returns>
    /// return min d where a^d = 1 (mod m)
    /// </returns>
    private long MultiplicativeOrder(IEnumerable<long> primes, long a, long m) {
        var phi = Phi(m);
        var res = phi;
        foreach (var p in primes) {
            if (p > phi) break;
            while (res % p == 0 && BigInteger.ModPow(a, res / p, m) == 1) {
                res /= p;
                phi /= p;
            }
            while (phi % p == 0) phi /= p;
        }
        if (phi > 1 && BigInteger.ModPow(a, res / phi, m) == 1) {
            res /= phi;
        }
        return res;
    }

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 0;
        // x^phi(m) = 1 (mod m)
        var primes = Factorize(m);
        var len = primes.Count;
        for (int i = 0; i < len; i++) {
            primes.AddRange(Factorize(primes[i] - 1));
        }
        primes.Sort();
        primes = primes.Distinct().ToList();

        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            var phi = Phi(m2);
            ans += phi / MultiplicativeOrder(primes, x, m2);
        }
        return ans;
    }

    public static long ModPow(long x, long n, long mod) {
        long t = x, ret = 1;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret = (long)(((decimal)t * ret) % mod);
            }
            t = (long)((decimal)t * t % mod);
            n >>= 1;
        }
        return ret;
    }

    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    Dictionary<long, List<long>> cache = new Dictionary<long, List<long>>();

    public List<long> Divisors(PrimeSet primes, long x) {
        //if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long> { 1 };
        foreach (var p in primes) {
            if (p * p > x) break;
            int last = 0;
            while (x % p == 0) {
                int count = ret.Count;
                for (int i = last; i < count; i++) {
                    ret.Add(ret[i] * p);
                }
                last = count;
                x /= p;
            }
        }
        if (x != 1) {
            int count = ret.Count;
            for (int i = 0; i < count; i++) {
                ret.Add(ret[i] * x);
            }
        }
        cache[x] = ret;
        return ret;
    }

    public List<long> Divisors(long x) {
        if (cache.TryGetValue(x, out var res)) return res;
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        cache[x] = ret;
        return ret;
    }

}

public class PrimeSet : List<int> {
    private readonly bool[] isPrime;

    public PrimeSet(int size)
        : base() {
        isPrime = Enumerable.Repeat(true, size).ToArray();
        Add(2);
        for (int i = 3; i <= size; i += 2) {
            if (!isPrime[i >> 1]) {
                continue;
            }

            Add(i);
            for (long j = (long)i * i; j <= size; j += i + i) {
                isPrime[j >> 1] = false;
            }
        }
    }

    public bool IsPrime(long x) {
        if (x == 2) {
            return true;
        }

        if (x <= 1 || x % 2 == 0) {
            return false;
        }

        return isPrime[x >> 1];
    }
}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Linq;

using E = System.Linq.Enumerable;
using System.Threading;

internal partial class Solver {
    public void Run() {
        var m = nl();
        var x = nl();

        cout.WriteLine(Solve(m, x));
    }

    /*
    Sample 1
    0 -> 0
    1 -> 3 -> 1 -> 3
    2 -> 2 -> 2 -> 2 (2*3^x%4 --> 3^x%2 と等化)
    3 -> 1 -> 3 -> 1

    g = gcd(start, M) ごとに場合分けすればよい
    start' = start / g
    M' = M / g
    start, start * x, ... start * x^k % M ->
    (start/g), start, ... start * x^k % M')
    start', start' * x, ... start' * x^k % M'

     * */

    private long Solve(long m, long x) {
        // from i to (i*x)%m
        long ans = 1; // for 0-room

        // x^phi(m) = 1 (mod m)
        foreach (var g in Divisors(m)) {
            var m2 = m / g;
            long minPower = long.MaxValue;
            foreach (var p in Divisors(Phi(m2))) {
                if (ModPow(x, p, m2) == 1) {
                    minPower = p;
                    break;
                }
            }
            var startNum = (m - 1) / g;
            ans += startNum / minPower;
        }
        return ans;
    }

    public static long ModMultiple(long x, long n, long mod) {
        long t = x, ret = 0;
        while (n > 0) {
            if ((n & 1) != 0) {
                ret += t;
                if (ret >= mod) ret -= mod;
            }
            t <<= 1;
            if (t >= mod) t -= mod;
            n >>= 1;
        }
        return ret;
    }

    public static long ModPow(long x, long n, long mod) {
        long t = x, ret = 1;
        t %= mod;
        while (n > 0) {
            if ((n & 1) != 0) {
                //ret = (t * ret) % mod;
                ret = ModMultiple(t, ret, mod);
            }
            //t = t * t % mod;
            t = ModMultiple(t, t, mod);
            n >>= 1;
        }
        return ret;
    }

    public static long Phi(long n) {
        long phi = n;

        for (long i = 2; i * i <= n && i < n; i++) {
            if (n % i == 0) {
                while (n % i == 0) {
                    n /= i;
                }

                phi = phi / i * (i - 1);
            }
        }
        if (n > 1) {
            phi = phi / n * (n - 1);
        }

        return phi;
    }

    public static List<long> Divisors(long x) {
        var ret = new List<long>();
        for (long i = 1; i * i <= x; i++) {
            if (x % i == 0) {
                ret.Add(i);
                if (i != x / i) {
                    ret.Add(x / i);
                }
            }
        }
        ret.Sort();
        return ret;
    }

}

// PREWRITEN CODE BEGINS FROM HERE

static public class StringExtensions {
    static public string JoinToString<T>(this IEnumerable<T> source, string separator = " ") {
        return string.Join(separator, source);
    }
}

internal partial class Solver : Scanner {
    static readonly int? StackSizeInMebiByte = null; //50;
    public static void StartAndJoin(Action action, int maxStackSize) {
        var thread = new Thread(new ThreadStart(action), maxStackSize);
        thread.Start();
        thread.Join();
    }

    public static void Main() {
#if LOCAL
        byte[] inputBuffer = new byte[1000000];
        var inputStream = Console.OpenStandardInput(inputBuffer.Length);
        using (var reader = new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length)) {
            Console.SetIn(reader);
            new Solver(Console.In, Console.Out).Run();
        }
#else
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        if (StackSizeInMebiByte.HasValue) {
            StartAndJoin(() => new Solver(Console.In, Console.Out).Run(), StackSizeInMebiByte.Value * 1024 * 1024);
        } else {
            new Solver(Console.In, Console.Out).Run();
        }
        Console.Out.Flush();
#endif
    }

#pragma warning disable IDE0052
    private readonly TextReader cin;
    private readonly TextWriter cout;
    private readonly TextWriter cerr;
#pragma warning restore IDE0052

    public Solver(TextReader reader, TextWriter writer)
        : base(reader) {
        cin = reader;
        cout = writer;
        cerr = Console.Error;
    }

    public Solver(string input, TextWriter writer)
        : this(new StringReader(input), writer) {
    }

#pragma warning disable IDE1006
#pragma warning disable IDE0051
    private int ni() { return NextInt(); }
    private int[] ni(int n) { return NextIntArray(n); }
    private long nl() { return NextLong(); }
    private long[] nl(int n) { return NextLongArray(n); }
    private double nd() { return NextDouble(); }
    private double[] nd(int n) { return NextDoubleArray(n); }
    private string ns() { return Next(); }
    private string[] ns(int n) { return NextArray(n); }
#pragma warning restore IDE1006
#pragma warning restore IDE0051
}

#if DEBUG
internal static class LinqPadExtension {
    public static string TextDump<T>(this T obj) {
        if (obj is IEnumerable) return (obj as IEnumerable).Cast<object>().JoinToString().Dump();
        else return obj.ToString().Dump();
    }
    public static T Dump<T>(this T obj) {
        return LINQPad.Extensions.Dump(obj);
    }
}
#endif

public class Scanner {
    private readonly TextReader Reader;
    private readonly CultureInfo ci = CultureInfo.InvariantCulture;

    private readonly char[] buffer = new char[2 * 1024];
    private int cursor = 0, length = 0;
    private string Token;
    private readonly StringBuilder sb = new StringBuilder(1024);

    public Scanner()
        : this(Console.In) {
    }

    public Scanner(TextReader reader) {
        Reader = reader;
    }

    public int NextInt() { return checked((int)NextLong()); }
    public long NextLong() {
        var s = Next();
        long r = 0;
        int i = 0;
        bool negative = false;
        if (s[i] == '-') {
            negative = true;
            i++;
        }
        for (; i < s.Length; i++) {
            r = r * 10 + (s[i] - '0');
#if DEBUG
            if (!char.IsDigit(s[i])) throw new FormatException();
#endif
        }
        return negative ? -r : r;
    }
    public double NextDouble() { return double.Parse(Next(), ci); }
    public string[] NextArray(int size) {
        string[] array = new string[size];
        for (int i = 0; i < size; i++) {
            array[i] = Next();
        }

        return array;
    }
    public int[] NextIntArray(int size) {
        int[] array = new int[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextInt();
        }

        return array;
    }

    public long[] NextLongArray(int size) {
        long[] array = new long[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextLong();
        }

        return array;
    }

    public double[] NextDoubleArray(int size) {
        double[] array = new double[size];
        for (int i = 0; i < size; i++) {
            array[i] = NextDouble();
        }

        return array;
    }

    public string Next() {
        if (Token == null) {
            if (!StockToken()) {
                throw new Exception();
            }
        }
        var token = Token;
        Token = null;
        return token;
    }

    public bool HasNext() {
        if (Token != null) {
            return true;
        }

        return StockToken();
    }

    private bool StockToken() {
        while (true) {
            sb.Clear();
            while (true) {
                if (cursor >= length) {
                    cursor = 0;
                    if ((length = Reader.Read(buffer, 0, buffer.Length)) <= 0) {
                        break;
                    }
                }
                var c = buffer[cursor++];
                if (33 <= c && c <= 126) {
                    sb.Append(c);
                } else {
                    if (sb.Length > 0) break;
                }
            }

            if (sb.Length > 0) {
                Token = sb.ToString();
                return true;
            }

            return false;
        }
    }
}