#include <bits/stdc++.h>
using namespace std;
#define faster ios::sync_with_stdio(5+2==2006); cin.tie(0); cout.tie(0);
#define ll long long
#define maxn
struct matrix{
    ll a[2][2];
};
ll mod, a, b;
ll mul(ll a, ll b){
    ll res=0;
     a%=mod;
    while (b){
        if (b%2){
            res+=a;
        }
        a*=2;
        a%=mod;
        b/=2;
    }
    return res;
}
matrix mul_matrix(matrix a, matrix b){
    matrix res={0,0,0,0};
    for (int i=0;i<2;i++){
        for (int j=0;j<2;j++){
            for (int k=0;k<2;k++){
                res.a[i][j]+=mul(a.a[i][k], b.a[k][j]);
                res.a[i][j]%=mod;
            }
        }
    }
    return res;
}
matrix powmatrix(matrix a, ll b){
    matrix res={1,0,0,1};
    while (b){
        if (b%2){
            res=mul_matrix(res,a);
        }
        a=mul_matrix(a,a);
        b/=2;
    }
    return res;

}
int main(){
    faster;
    cin>>a>>b>>mod;
    ll n=__gcd(a,b);
    matrix f={1,1,1,0};
    f=powmatrix(f,n);
    cout<<f.a[1][0];



    return 0;
}
