def pisano_period(m): if m == 1: return 1 previous, current = 0, 1 period = 0 for _ in range(m * 6): previous, current = current, (previous + current) % m period += 1 if previous == 0 and current == 1: return period return period # Fallback, theoretically shouldn't reach here def fib_mod_r(r, m): if r == 0: return 0 a, b = 0, 1 for _ in range(r - 1): a, b = b, (a + b) % m return b n, m = map(int, input().split()) if m == 1: print(0) else: period = pisano_period(m) r = n % period print(fib_mod_r(r, m))