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
#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
int main() {
	ll n, x, m,ans=0,flag=-1,pan=1,t=0,tt=0,k=150000;
	cin >> n >> x >> m;
	ll xx = x;
	for (ll i = 0; i < n; i++) {
		int p = 1;
		ans += x;
		x = x * x;	
		if (k > 1)k--;
		if (pan==1 &&flag==-1&&k==1)pan = 0, flag = x % m,p=0;
		x %= m;
		if (p && !pan && x == flag) {
			ll ttt = ((n - 1 - i) / tt);
			ans += ttt * t;
			i += ttt * tt;
			pan = 2;
			}
			else if (!pan) { t += x, tt++; }
	}
	cout << ans << endl;
	return 0;
}