import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int ans = 2017 % m + pow(pow(2017 % m, 2, m), 2017, m); ans %= m; System.out.println(ans); } static int pow(int x, int p, int m) { if (p == 0) { return 1; } else if (p % 2 == 0) { return pow(x * x % m, p / 2, m); } else { return pow(x, p - 1, m) * x % m; } } }