import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines M = int(read()) x = 2017 answer = x + pow(x*x, 2017, M) answer %= M print(answer)