import sys import math import itertools from collections import defaultdict, deque, Counter from copy import deepcopy from bisect import bisect, bisect_right, bisect_left from heapq import heapify, heappop, heappush from operator import itemgetter, attrgetter input = sys.stdin.readline def RD(): return input().rstrip() def F(): return float(input().rstrip()) def I(): return int(input().rstrip()) def MI(): return map(int, input().split()) def MF(): return map(float,input().split()) def LI(): return list(map(int, input().split())) def TI(): return tuple(map(int, input().split())) def LF(): return list(map(float,input().split())) def Init(H, W, num): return [[num for i in range(W)] for j in range(H)] def TL(mylist): return [list(x) for x in zip(*mylist)] #行と列入れ替え def RtoL(mylist): return [list(reversed(x)) for x in mylist] #左右反転 def HtoL(mylist): return [x for x in list(reversed(mylist))] #上下反転 def convert_2d(l, colstart, colend, rawstart, rawend):return [i[rawstart:rawend] for i in l[colstart:colend]] #2次元行列から一部を採取 def get_unique_list(seq): seen = [] return [x for x in seq if x not in seen and not seen.append(x)] # (an+3, an+2) = (2, 1, 1, 1)(an+1, an) N, M = MI() def mat_mul(a, b) : I, J, K = len(a), len(b[0]), len(b) c = [[0] * J for _ in range(I)] for i in range(I) : for j in range(J) : for k in range(K) : c[i][j] += a[i][k] * b[k][j] c[i][j] %= M return c def mat_pow(x, n): y = [[0] * len(x) for _ in range(len(x))] for i in range(len(x)): y[i][i] = 1 while n > 0: if n & 1: y = mat_mul(x, y) x = mat_mul(x, x) n >>= 1 return y def main(): N2 = (N-1) // 2 X =[[2,1],[1,1]] Y = mat_pow(X, N2) a1 = 0 a2 = 1 if N % 2 == 0: print(Y[0][0]*a2+Y[0][1]*a1) else: print(Y[1][0]*a2+Y[1][1]*a1) if __name__ == "__main__": main()