m = int(input()) ans = pow(pow(2017, 2, m), 2017, m) ans += 2017 % m print(ans % m)