#include using namespace std; typedef long long ll; ll modPow(ll x, ll n, ll mod) { if (n == 0) return 1; ll res = modPow(x * x % mod, n >> 1, mod); if (n & 1) res = res * x % mod; return res; } int main(void) { ll m; cin >> m; cout << (2017 + modPow(2017, 2017 * 2, m)) % m << endl; return 0; }