#include using namespace std; using ll = long long; #include using mint = atcoder::modint; int main(){ ll N, M; cin >> N >> M; if (M <= 2){ cout << (N == 1 ? 2 : 0) << "\n"; return 0; } mint::set_mod((int)N); mint a = 1, b = 1; vector q; do{ a += b; swap(a, b); q.push_back(a.val()); }while (a != 1 || b != 1); 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"; }