import numpy as np def fib(num1, num2): A = np.array([[1,1], [1,0]]) ans = np.array([[1,0],[0,1]]) for i in bin(num1)[::-1][:-2]: if i == '1': ans = np.dot(ans, A) % num2 A = np.dot(A,A) % num2 return ans[1][1] N, M = [int(i) for i in input().strip().split(' ')] print(fib(N, M))