#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
int a,b,p;
signed main()
{
cin>>a>>b>>p;
int res = 0;
while(b)
{
if(b&1)res = (res + a)%p;
b >>= 1;
a = (a + a)%p;
}
cout<<res<<endl;
return 0;
}