N, M = map(int, input().split()) if M <= 2: print(2 if N == 1 else 0) exit() a = 1; b = 1 fib = [] while True: a = (a + b) % N a, b = b, a fib.append(a) if a == 1 and b == 1: break p = [0] * N L = len(fib) syo = (M - 1) // L rem = (M - 1) % L for i in range(L): p[fib[i]] += syo + (1 if i < rem else 0) ans = 0 if fib[(M - 2) % L] == 0: ans += 2 if fib[(M - 3) % L] == 0: ans += 1 for x in p: ans += x * (x - 1) // 2 print(ans)