#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 <= 2){ cout << (n <= 1) + (n <= 2) << '\n'; return 0; } if(n == 1){ ll len = m - 2; cout << len * (len + 1) / 2 + 2 << '\n'; return 0; } 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 % r] == 0) * 2; for(auto &&v : cnt) ans += (ll)(v) * (v - 1) / 2; cout << ans << '\n'; }