N, M = list(map(int, input().split())) F = {0: 0, 1: 1} for i in range(2,N): temp = F[i-1] + F[i-2] if temp == M and F[i-1] == 1: mod_n = N % i print(F[mod_n]) exit() elif temp >= M: F[i] = temp - M else: F[i] = temp else: print(F[N-1])