#include using namespace std; using ll = long long; int main(){ ll N, M; cin >> N >> M; if (M <= 2){ cout << (N == 1 ? 2 : 0) << "\n"; return 0; } if (N == 1){ cout << 3 + (M - 2) * (M - 1) / 2 << "\n"; return 0; } ll a = 1, b = 1; vector q; do{ a = (a + b) % N; swap(a, b); q.push_back(a); }while (a != 1 || b != 1); // f2, f3, ... , fm vector p(N); int len = q.size(); int syo = (M - 1) / len; int rem = (M - 1) % len; for (int i = 0; i < len; i++){ p[q[i]] += syo + (i < rem); } ll ans = 0; if (q[(M - 2) % len] == 0) ans += 2; if (q[(M - 3) % len] == 0) ans += 1; for (auto x : p) ans += x * (x - 1) / 2; cout << ans << "\n"; }