M = int(input()) if M == 2017:print(0) else: q = 2017 // M u = 2017 % M print((u+u ** (2 * q + 2 * u)) % M)