import strutils,sequtils var m : int64 type Matrix[H,W: static[int]] = array[1..H,array[1..W,int64]] proc `+`[H,W](a,b:Matrix[H,W]):Matrix[H,W] = for i in 1..high(a): for j in 1..high(a[0]): result[i][j] = a[i][j] + b[i][j] proc `*`[Ha,Wa,Wb](a:Matrix[Ha,Wa],b:Matrix[Wa,Wb]):Matrix[Ha,Wb] = var k : int64 for i in 1.. Ha: for j in 1..Wb: k = 0 for ai in 1..Wa: k = (k + a[i][ai] * b[ai][j]) mod m result[i][j] = k proc powMatrix[W,H](a : Matrix[W,H], n : int64):Matrix[W,H] = if n == 1: return a if n mod 2 == 0: return powMatrix(a * a,n div 2) else: return powMatrix(a * a,n div 2) * a proc printMatrix(A: Matrix)= for a in A: echo a.join(", ") proc fib(n : int64): int64 = if n == 0: return 0 var A : Matrix[2,2] = [[1.int64,1.int64],[1.int64,0.int64]] return (powMatrix(A,n))[2][1] var N,ans : int64 L : seq[int64] L = stdin.readline.split.map(parseBiggestInt) (N, m) = (L[0],L[1]) ans = fib(N - 1) echo ans