#ifdef NACHIA #define _GLIBCXX_DEBUG #else #define NDEBUG #endif #include <iostream> #include <string> #include <vector> #include <algorithm> using i64 = long long; using u64 = unsigned long long; #define rep(i,n) for(int i=0; i<int(n); i++) const i64 INF = 1001001001001001001; template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; } template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; } using namespace std; pair<i64,i64> 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; b = u * (v-u) + v * u; b %= m; } if(n % 2 == 1){ swap(a, b); b += a; } a %= m; b %= m; return { a, b }; } void testcase(){ i64 N, M; cin >> N >> M; if(N == 1){ if(M == 1){ cout << "1\n"; } else if(M == 2){ cout << "2\n"; } else { cout << ((M-2) * (M-1) / 2 + 3) << '\n'; } return; } 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<i64> 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<i64>& f, const vector<i64>& g, i64 t) -> vector<i64> { vector<i64> res = f; rep(i,N) res[i*t%N] += g[i]; return res; }; auto powT = [&](const vector<i64>& a, i64 t, i64 m){ vector<i64> res(N); vector<i64> 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<i64> freq(N); rep(i,C) freq[F[i]] += 1; auto [G, m2] = powT(freq, T, (M-0)/C); //cout << "C = " << C << endl; //cout << "len = " << ((M-0)/C) << endl; //cout << "m2 = " << m2 << endl; rep(q,(M-0)%C) G[F[q]*m2%N] += 1; G[1] -= 1; rep(i,N) ans += G[i] * (G[i]-1) / 2; } cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); testcase(); return 0; }