#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, m, p; cin >> n >> m; if(m <= 60){ vector a = {1, 1}; while(a.size() < m) a.emplace_back(a.rbegin()[1] + a.back()); int ans = 0; for(int l = 0; l < m; l++){ ll sv = 0; for(int r = l; r < m; r++){ sv += a[r]; if(sv > a.back()) break; ans += sv % n == 0; } } cout << ans << '\n'; exit(0); } return 1; if(n == 1){ ll len = m - 2; cout << len * (len + 1) / 2 + 2 << '\n'; return 0; } return 1; vector a = {1, 1}, cnt(n); a.reserve(2 * n); do{ a.emplace_back(a.rbegin()[1] + a.back()); if(a.back() >= n) a.back() -= n; }while(a.rbegin()[1] != 1 || a.back() != 1); a.pop_back(); a.pop_back(); const int r = a.size(), d = (m - 2) / r, rem = (m - 2) - r * d; int sv = 0; cnt[0]++; for(int i = 0; i < r; i++){ sv += a[i]; if(sv >= n) sv -= n; cnt[sv] += d; if(i < rem) cnt[sv]++; } ll ans = (a[(m - 1) % r] == 0) * 2; for(auto &&v : cnt) ans += (ll)(v) * (v - 1) / 2; cout << ans << '\n'; }