import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### def inv_gcd(a, b): a %= b if a == 0: return b, 0 # 初期状態 s, t = b, a m0, m1 = 0, 1 while t: # 遷移の準備 u = s // t # 遷移 s -= t * u m0 -= m1 * u # swap s, t = t, s m0, m1 = m1, m0 if m0 < 0: m0 += b // s return s, m0 def crt(r, m): assert len(r) == len(m) n = len(r) r0, m0 = 0, 1 # 初期値 x = 0 (mod 1) for i in range(n): assert m[i] >= 1 #r1, m1は遷移に使う値 r1, m1 = r[i] % m[i], m[i] #m0がm1以上になるようにする。 if m0 < m1: r0, r1 = r1, r0 m0, m1 = m1, m0 # m0がm1の倍数のとき gcdはm1、lcmはm0 # 解が存在すれば何も変わらないので以降の手順はスキップ if m0 % m1 == 0: if r0 % m1 != r1: return [0, 0] continue # 拡張ユークリッドの互除法によりgcd(m0, m1)と m0 * im = gcd (mod m1) を満たす imを求める g, im = inv_gcd(m0, m1) # 解の存在条件の確認 if (r1 - r0) % g: return [0, 0] """ r0, m0の遷移 コメントアウト部分はACLでの実装 C++なのでlong longを超えないようにしている C++ はlcm(m0, m1)で割った余りが負になり得る """ # u1 = m1 // g # x = (r1 - r0) // g % u1 * im % u1 # r0 += x * m0 # m0 *= u1 u1 = m0 * m1 // g r0 += (r1 - r0) // g * m0 * im % u1 m0 = u1 #if r0 < 0: r0 += m0 return [r0, m0] def convert(x, base): digits = [] for _ in range(n): digits.append(x % base) x //= base return digits[::-1] def inv(n, base): res = 0 for i in n: res = res * base + i return res n, m = na() B = [] C = [] Q = [] R = [] S = [] for i in range(3**n): x = convert(i, 3) B.append(inv(x, 4)) C.append(inv(x, 6)) Q.append((C[i] - B[i])%(2**n)) R.append((C[i] - i)%(3**n)) S.append(Q[i] + R[i] * (2**n)) from collections import defaultdict res = defaultdict(list) o = dict() for i in range(3**n): if not S[i] in o: o[S[i]] = i crt_res = crt([i-o[S[i]], B[i] - B[o[S[i]]], C[i] - C[o[S[i]]]], [3**n, 4**n, 6**n]) #print(crt_res) assert crt_res[1] != 0 res[S[i]].append(crt_res[0]) #print(res) ans = 0 for i in res: x = res[i] x.sort() x.append(12**n) for j in range(len(x)-1): ans += x[j+1] - x[j] > m print(ans)