#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; vector> a[16]; } int main() { ios::sync_with_stdio(false); cin.tie(0); a[15] = { {{1,1,1,1,2567836929097726,12839184645488638}, 1594323}, {{1,1,15407021574586366}, 1594323} }; a[14] = {{{1,1,1283918464548862}, 1594323}}; a[13] = { {{1,1,1,1,17832200896510,89161004482558}, 177147}, {{1,1,106993205379070}, 177147} }; a[12] = { {{1,1,1,1,2105190383614,6810910064638}, 39366}, {{1,1,8916100448254}, 98415} }; a[11] = { {{1,1,1,1,123834728446,619173642238}, 19683}, {{1,1,743008370686}, 19683} }; a[10] = { {{1,1,1,1,16339304446,45578059774}, 4374}, {{1,1,61917364222}, 10935} }; a[9] = { {{1,1,1,1,859963390,4299816958}, 2187}, {{1,1,5159780350}, 2187} }; a[8] = { {{1,1,1,1,80885662,349096030}, 108}, {{1,1,1,1,182409118,247572574}, 18}, {{1,1,1,1,194353054,235628638}, 18}, {{1,1,429981694}, 1899}, }; a[7] = { {{1,1,1,1,1,1,1,1,3250078,5971966,5971966,20637790}, 18}, {{1,1,1,1,5971966,29859838}, 207}, {{1,1,1,1,9222046,26609758}, 18}, {{1,1,35831806}, 207} }; a[6]={{{1,1,1,1,1,1,264094,705022,2016862}, 12}, {{1,1,1,1,264094,2721886}, 6}, {{1,1,1,1,705022,2280958}, 42}, {{1,1,2985982}, 111}}; a[5]={{{1,1,1,1,1,1,1,1,15262,15262,26206,192094}, 6}, {{1,1,1,1,15262,233566}, 6}, {{1,1,1,1,41470,207358}, 15}, {{1,1,248830}, 15}}; a[4]={{{1,1,1,1,5470,15262}, 6}, {{1,1,20734}, 15}}; a[3]={{{1,1,1,1,286,1438}, 3}, {{1,1,1726}, 3}}; a[2]={{{1,1,142}, 3}}; a[1]={{{1,1,10}, 1}}; int pow3[16]; pow3[0] = 1; rep(i, 15) pow3[i+1] = pow3[i] * 3; ll pow6[16]; pow6[0] = 1; rep(i, 15) pow6[i+1] = pow6[i] * 6; // for (int i = 2; i <= 15; i++) { // ll tot = a1[i]; // for (auto [d, cnt] : a2[i]) tot += cnt * 2; // assert(tot == pow3[i]); // } int n; ll m; cin >> n >> m; const ll period = (ll)pow3[n] << (2 * n); int ans = 0; for (auto& [d, cnt] : a[n]) for (auto d : d) if (m < d) ans += cnt; cout << ans << endl; // if (m >= period) { // cout << 0 << '\n'; // return 0; // } // if (n == 1) { // cout << int(m < 10) << '\n'; // return 0; // } // int ans = a1[n]; // for (auto [d, cnt] : a2[n]) { // if (d < m) ans += cnt; // if (period - d < m) ans += cnt; // } // cout << ans << '\n'; // unordered_map mp; // rep(s, pow3[n]) { // int t = 0; // ll u = 0; // int tmp = s; // rep(i, n) { // int r = tmp % 3; // tmp /= 3; // t += (r << (2 * i)); // u += r * pow6[i]; // } // static VL r, m; // r = {s, t}; // m = {pow3[n], 1 << (2 * n)}; // auto [y, z] = crt(r, m); // assert(z == (ll)pow3[n] << (2 * n)); // ll offset = (u - y) % pow6[n]; // if (offset < 0) offset += pow6[n]; // mp[offset].emplace_back(y); // } // map dup; // for (auto& [k, v] : mp) { // // if (v.size() >= 2) { // // assert(v.size() == 2); // // sort(all(v)); // // ll x1 = v[0], x2 = v[1]; // // ll d = x2 - x1; // // cout << d << ','; // // } // static VL tmp; // tmp.clear(); // sort(all(v)); // int sz = v.size(); // rep(i, sz - 1) tmp.emplace_back(v[i+1] - v[i]); // tmp.emplace_back(period + v[0] - v[sz-1]); // sort(all(tmp)); // dup[tmp]++; // } // cout << '\n'; // for (auto& [k, v] : dup) { // cout << "{{"; // bool first = true; // for (auto t : k) { // if (!first) cout << ','; // cout << t; // first = false; // } // cout << "}, " << v << "},\n"; // } }