m = int(input()) def pow_mod(x, n, m): if n == 0: return 1 if x % 2 == 0: return pow(x * x, n / 2, m) % m else: return x * pow(x, n - 1, m) ans = (2017 + pow_mod(2017 * 2017, 2017, m)) % m print(ans)