#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; using ll = long long; using pll = pair; const ll mod = 998244353; int n; ll extgcd(ll a, ll b) { ll x0 = 1, x1 = 0, y0 = 0, y1 = 1; while (b != 0) { ll q = a / b; ll r = a % b; ll x2 = x0 - q * x1; ll y2 = y0 - q * y1; a = b; b = r; x0 = x1; x1 = x2; y0 = y1; y1 = y2; } return x0; } ll crt(ll r1, ll m1, ll r2, ll m2, ll dpq1) { ll m = m1 * m2; ll temp = (r2 - r1) * dpq1 % m2; return ((r1 + m1 * temp) % m + m) % m; } ll pw(ll a, ll b) { ll x = 1; while (b > 0) { if ((b & 1) == 1) { x = x * a; } a = a * a; b >>= 1; } return x; } int main() { cin.tie(0)->sync_with_stdio(false); cin >> n; ll m; cin >> m; m += 2; const ll p3 = pw(3, n); const ll p4 = pw(4, n); const ll p6 = pw(6, n); const ll p12 = pw(12, n); ll dpq1 = extgcd(p3, p4); map first; map last; int ans = 0; for (ll v3 = 0; v3 < p3; v3 += 3) { ll v4 = 0; ll v6 = 0; ll tmp = v3; ll pc4 = 4; ll pc6 = 6; for (int i = 1; i < n; i++) { tmp /= 3; v4 += tmp % 3 * pc4; v6 += tmp % 3 * pc6; pc4 *= 4; pc6 *= 6; } ll v12 = crt(v3, p3, v4, p4, dpq1); ll diff = (v12 + p6 - v6) % p6; if (first.count(diff)) { if (v12 - m > last[diff]) { ans++; } last[diff] = v12; } else { first[diff] = v12; last[diff] = v12; } } for (auto &[k, vv]: first) { if (last[k] - m > vv - p12) { ans++; } } cout << ans << endl; }