import java.util.Scanner; public class Calc2017 { static int M; public static void main(String[] args) { Scanner s = new Scanner(System.in); M = s.nextInt(); s.close(); int a = 2017%M; int b; b = Rest(a,4034); a = (a + b)%M; System.out.println(a); } static int Rest(int a,int K){ int Q,r,Ans = 0,b; for(int i = 1;i <= K;i++){ if(Math.pow(a,i) >=M){ Q = K/i; r = K%i; b = (int)Math.pow(a, i)%M; Ans = (Rest(b,Q) * Rest(a,r)) % M; return Ans; } } return (int)Math.pow(a, K); } }