from copy import deepcopy def matpow(A, B, w): l = len(A) while w: if w & 1: C = [0] * l for i in range(l): for j in range(l): C[i] += A[i][j] * B[j] C[i] %= MOD B = C.copy() C = [[0] * l for _ in range(l)] for i in range(l): for j in range(l): for k in range(l): C[i][j] += A[i][k] * A[k][j] C[i][j] %= MOD A = deepcopy(C) w >>= 1 return B n, MOD = map(int, input().split()) A = [[1, 1], [1, 0]] B = [1, 0] C = matpow(A, B, n - 1) print(C[1])