N, M = list(map(int, input().split())) F = [None for _ in range(N)] F[0:1] = [0,1] for i in range(2,N): temp = F[i-1] + F[i-2] if temp == 0: print(0) break elif temp >= M: F[i] = temp - M else: F[i] = temp else: print(F[N-1])