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;
long long a[100010],x,m,n,cnt,blag,cx=0,t;
long long ans=0,res,jyz[100010]={0};
int main()
{
	cin>>n>>x>>m;
	memset(a,0,sizeof a);
	res=x;ans=res;jyz[1]=res;a[res]=1;
	for (register int i=2;i<=n;i++) 
	{
		res=res*res;
		if (res>=m) res%=m;
		//if (res==0) break;
		if (a[res]!=0) {cnt=i-a[res];blag=a[res];jyz[i]=jyz[i-1]+res;t=i;cx=233;break;}
		else {a[res]=i;jyz[i]=jyz[i-1]+res;ans+=res;}
	}
	if (cx==233) 
	{
		ans+=(n-t)/cnt*(jyz[t]-jyz[blag]); 
		for (register int i=1;i<a[res];i++) x=x*x%m;
		for (register int i=a[res];i<a[res]+(n-t)%cnt;i++) ans+=x,x=x*x%m;
		if (t!=n) ans+=x;
	}  
	cout<<ans<<endl;
	return 0;
} 