print((function (m) local t,r=2017%m,2017%m for _=2,2*2017 do r=(r*t)%m end return (t+r)%m end)(io.stdin:read("*n")))