Let us denote by f(x, m) the remainder of the Euclidean division of x by m.

Let A be the sequence that is defined by the initial value A_1=X and the recurrence relation A_{n+1} = f(A_n^2, M). Find \displaystyle{\sum_{i=1}^N A_i}.

Constraints

* 1 \leq N \leq 10^{10}
* 0 \leq X < M \leq 10^5
* All values in input are integers.

Input

Input is given from Standard Input in the following format:


N X M


Output

Print \displaystyle{\sum_{i=1}^N A_i}.

Examples

Input

6 2 1001


Output

1369


Input

1000 2 16


Output

6


Input

10000000000 10 99959


Output

492443256176507
// E - Sequence Sum
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char *argv[])
{
	long long n, x, m, c = 0, l = 0;
	cin >> n >> x >> m;
	long long ans = 0;
	map<long long, int> s;
	for (c = 0; c < n; c++) {
		if (s.count(x) > 0) {
			l = c - s[x];
			break;
		}
		ans += x;
		s[x] = c;
		x = (x * x) % m;
	}

	long long z = 0;
	for (long long t = x, i = 0; i < l; i++, t = (t * t) % m) z += t;

	if (l > 0) {
		ans += (n - c) / l * z;
		for (long long i = 0, t = x; i < (n - c) % l; i++, t = (t * t) % m)
			ans += t;
	}

	cout << ans << endl;

	return 0;
}
