n, x, m = list(map(int, input().split()))



lst, num = set(), []

for i in range(1, n + 1):

    lst.add(x), num.append(x)

    x = x ** 2 % m

    if x in lst:

        break



ans = sum(num)

if x in lst:

    cnt, idx = i, num.index(x)

    div, mod = divmod(n - cnt, len(num) - idx)

    ans += sum(num[idx:idx + mod])

    ans += sum(num[idx:]) * div

print(ans)

n, x, m = list(map(int, input().split()))



lst, num = set(), []

for i in range(n):

    lst.add(x), num.append(x)

    x = x ** 2 % m

    if x in lst:

        cnt, idx = i + 1, num.index(x)

        break



ans = sum(num)

if x in lst:

    div, mod = divmod(n - cnt, len(num) - idx)

    ans += sum(num[idx:]) * div

    ans += sum(num[idx:idx + mod])

print(ans)

n, x, m = list(map(int, input().split()))
lst, num = set(), []
for i in range(1, n + 1):
    lst.add(x), num.append(x)
    x = x**2 % m
    if x in lst:
        break
ans = sum(num)
if x in lst:
    cnt, idx = i, num.index(x)
    div, mod = divmod(n - cnt, len(num) - idx)
    ans += sum(num[idx : idx + mod])
    ans += sum(num[idx:]) * div
print(ans)

n, x, m = list(map(int, input().split()))
lst, num = set(), []
for i in range(n):
    lst.add(x), num.append(x)
    x = x**2 % m
    if x in lst:
        cnt, idx = i + 1, num.index(x)
        break
ans = sum(num)
if x in lst:
    div, mod = divmod(n - cnt, len(num) - idx)
    ans += sum(num[idx:]) * div
    ans += sum(num[idx : idx + mod])
print(ans)

-for i in range(1, n + 1): +for i in range(n): +        cnt, idx = i + 1, num.index(x) -    cnt, idx = i, num.index(x) +    ans += sum(num[idx:]) * div -    ans += sum(num[idx:]) * div