N, M = map(int, input().split()) i = 1 if N <= 2: print (N) else: while i in M: s = s[i-1] +s[s - 2] print(N % M)