#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include #include #include #include using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i void chmin(A& l, const A& r){ if(r < l) l = r; } template void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; pair fib(i64 n, i64 m){ i64 a = 1; i64 b = 1; if(n >= 2){ auto [u,v] = fib(n/2, m); a = u * (u*2-v) + v * (v-u); a %= m; a += m; a %= m; b = u * (v-u) + v * u; b %= m; b += m; b %= m; } if(n % 2 == 1){ swap(a, b); b += a; } a %= m; b %= m; return { a, b }; } i64 solve(i64 N, i64 M){ if(N == 1){ if(M == 1) return 1; else if(M == 2) return 2; else return ((M-2) * (M-1) / 2 + 3); } i64 ans = 0; if(fib(M-1,N).first == 0) ans += 1; if(M >= 2 && fib(M-2,N).first == 0) ans += 1; if(M >= 3 && fib(M-1,N).first == 0) ans += 1; if(M >= 3){ vector F = { 1, 1 }; for(i64 i=2; ; i++){ if(F[i-1] == 0) break; F.push_back((F[i-1] + F[i-2]) % N); } i64 C = F.size(); i64 T = F[F.size()-2]; auto addT = [&](const vector& f, const vector& g, i64 t) -> vector { vector res = f; rep(i,N) res[i*t%N] += g[i]; return res; }; auto powT = [&](const vector& a, i64 t, i64 m){ vector res(N); vector aa = a; i64 tt = t; i64 m2 = 1; for(i64 v=1; v<=m; v*=2){ if(m&v){ res = addT(aa, res, tt); m2 = m2 * tt % N; } aa = addT(aa, aa, tt); tt = tt * tt % N; } return make_pair(res, m2); }; vector freq(N); rep(i,C) freq[F[i]] += 1; auto [G, m2] = powT(freq, T, (M-0)/C); rep(q,(M-0)%C) G[F[q]*m2%N] += 1; G[1] -= 1; rep(i,N) ans += G[i] * (G[i]-1) / 2; } return ans; } void testcase(){ i64 N, M; cin >> N >> M; i64 ans = solve(N, M); cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }