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