M = int(input()) p = 2017 ans = (p + pow(p, 2*p, M)) % M print(ans)