#include using namespace std; template< typename T = long long > T pow_mod(T x, T n, T mod) { T ret = 1; while(n > 0) { if(n & 1) ret = 1LL * ret * x % mod; x = 1LL * x * x % mod; n >>= 1; } return ret; } int main() { int M; cin >> M; cout << (2017 + pow_mod(2017 * 2017, 2017, M)) % M << endl; }