def calc(a, b, m): ar, ac, bc = len(a), len(a[0]), len(b[0]) res = [[0] * bc for _ in range(ar)] for i in range(ar): for j in range(bc): res[i][j] = sum([a[i][k] * b[k][j] for k in range(ac)]) % m return res n, m = map(int, input().split()) a = [[1, 1], [1, 0]] ans = [[1], [0]] k = n - 2 while k > 0: if k & 1: ans = calc(a, ans, m) a = calc(a, a, m) k >>= 1 print(ans[0][0])