using System;
using System.Linq;
using CompLib.Util;
using System.Threading;

public class Program
{
    long N, X, M;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextLong();
        X = sc.NextInt();
        M = sc.NextInt();

        var table = new int[M];
        long tmp = X;
        int e = 1;
        while (true)
        {
            if (table[tmp] != 0) break;
            table[tmp] = e;
            // Console.WriteLine($"{tmp} {e}");
            tmp *= tmp;
            tmp %= M;
            e++;
        }

        // ループまで
        // t[tmp]-1回
        // 開始 t[tmp]
        // ループ e-t[tmp]
        int b = table[tmp] - 1;

        long f = table[tmp];
        int loop = e - table[tmp];

        // Console.WriteLine($"{ e} {tmp}");

        long ans = 0;

        long tmp2 = X;
        for (int i = 0; i < b && N >= 0; i++, N--)
        {
            ans += tmp2;
            tmp2 *= tmp2;
            tmp2 %= M;
        }
        // Console.WriteLine(ans);
        // Console.WriteLine(b);
        // Console.WriteLine(tmp2);
        if(N == 0)
        {
            Console.WriteLine(ans);
            return;
        }

        long l = 0;
        for (int i = 0; i < loop; i++)
        {
            l += tmp2;
            tmp2 *= tmp2;
            tmp2 %= M;
        }
        
        long d = N / loop;
        long m = N % loop;
        ans += d * l;
        for (int i = 0; i < m; i++)
        {
            ans += tmp2;
            tmp2 *= tmp2;
            tmp2 %= M;
        }

        Console.WriteLine(ans);
        // Console.WriteLine(l);
    }

    public static void Main(string[] args) => new Program().Solve();
    // public static void Main(string[] args) => new Thread(new Program().Solve, 1 << 27).Start();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


Measured memory_bytes: 27904