#include using namespace std; #define FOR(i,l,r) for(int i = (int) (l);i < (int) (r);i++) #define ALL(x) x.begin(),x.end() template bool chmax(T& a,const T& b){ return a < b ? (a = b,true) : false; } template bool chmin(T& a,const T& b){ return b < a ? (a = b,true) : false; } typedef long long ll; ll N = 2017,M; ll pow_mod(ll x,ll y,ll mod) { ll res = 1; while(y){ if(y & 1) (res *= x) %= mod; (x *= x) %= mod; y >>= 1; } return res; } int main() { scanf("%lld",&M); ll ans = (N + pow_mod(N,N * 2,M)) % M; printf("%lld\n",ans); return 0; }