def dot(A,B,M): l = len(A) m = len(A[0]) if m != len(B): return False n = len(B[0]) C = [[] for _ in range(l)] for i in range(l): for j in range(n): sum = 0 for k in range(m): sum += A[i][k]*B[k][j] sum %= M C[i].append(sum) return C A = [[1,1],[1,0]] N,M = map(int,input().split()) N -= 1 now = [[1,0],[0,1]] l = len(bin(N)) - 2 for i in range(l): if N >> i & 1 == 1: now = dot(now,A,M) A = dot(A,A,M) print(now[1][0])