fn main() {
    input! {
        n: usize,
        m: usize,
    }
    if m <= 30 {
        let mut fib = vec![0; 101];
        fib[1] = 1;
        fib[2] = 1;
        let up = 40;
        for i in 3..=up {
            fib[i] = fib[i - 1] + fib[i - 2];
        }
        let mut ans = 0;
        for l in 1..=up {
            for r in l..=up {
                let s = fib[l..=r].iter().sum::<usize>();
                if s % n == 0 && s <= fib[m] {
                    ans += 1;
                }
            }
        }
        println!("{}", ans);
        return;
    }
    if n == 1 {
        let ans = (m - 1) * (m - 2) / 2 + 3;
        println!("{}", ans);
        return;
    }
    let mut f = (0, 1);
    let mut s = 0;
    let mut dp = vec![0usize; n];
    dp[s] += 1;
    let mut i = 1;
    while i < m - 1 {
        f = (f.1, (f.0 + f.1) % n);
        s = (s + f.0) % n;
        if f == (0, 1) && m >= 3 + i {
            assert!(s == 0);
            let q = (m - 3) / i;
            for dp in dp.iter_mut() {
                *dp *= q;
            }
            i += (q - 1) * i;
        }
        dp[s] += 1;
        i += 1;
    }
    f = (f.1, (f.0 + f.1) % n);
    let mut ans = 0;
    for &v in dp.iter() {
        ans += v * (v - 1) / 2;
    }
    if f.1 == 0 {
        ans += 2;
    }
    if f.0 == 0 {
        ans += 1;
    }
    println!("{}", ans);
}

// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[macro_export]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};
    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[macro_export]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };
    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };
    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };
    ($iter:expr, bytes) => {
        read_value!($iter, String).bytes().collect::<Vec<u8>>()
    };
    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };
    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// ---------- end input macro ----------