import sys import numpy as np stdin=sys.stdin ip=lambda: int(sp()) fp=lambda: float(sp()) lp=lambda:list(map(int,stdin.readline().split())) sp=lambda:stdin.readline().rstrip() Yp=lambda:print('Yes') Np=lambda:print('No') N, M = lp() A = [] basic = np.array([[0, 1],[1, 1]],dtype=int) A.append(basic) for i in range(40): now = A[-1] nex = np.dot(now,now) nex %= M A.append(nex) st = 39 gyouretu = np.eye(2,dtype=int) N -= 2 while N: if 2**st > N: st -= 1 continue else: N -= 2**st gyouretu = np.dot(gyouretu, A[st]) gyouretu %= M st -= 1 ans = np.dot(gyouretu, np.array([0, 1])) print(ans[1])