import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
    /// x -> (ax + b) % p
    static final int b = 1;
    static final int a = 618023;
    static final int p = 999983;
 
    static int solve(int n, int s) {
        int cnt = 0;
        
        long[] pref = new long[65];
        long[] suff = new long[65];
        
        for (int i = 1; i <= 64; i++)
            pref[i] = (pref[i - 1] | (1L << (i - 1)));
        for (int i = 1; i <= 64; i++)
            suff[i] = (suff[i - 1] | (1L << (64 - i)));
        
        final int len = (p + 64) / 64;
        long[] has_odd_reprs = new long[len];
        long[] is_on = new long[len];
        
        for (int iter = 0; iter < n; iter++)
        {
            s = (int)((a * 1L * s + b) % p);
            
            final int full = (s >> 6);
            final int part = (s & 63);
            
            if (((has_odd_reprs[full] >> part) & 1L) == 0L)
            {
                cnt++;
                if ((is_on[full] & (1L << part)) != 0L)
                    System.out.println("repeat " + Integer.toString(s));
                is_on[full] |= (1L << part);
                
                //for (int i = 0; i < p; i++)
                //if (((is_on[i >> 6] >> (i & 63)) & 1L) == 1L)
                //if (i + s < p)
                //    has_odd_reprs[(i + s) >> 6] ^= (1L << ((i + s) & 63));
                
                if (part == 0)
                {
                    for (int i = 0; i < len - full; i++)
                        has_odd_reprs[i + full] ^= is_on[i];
                }
                else
                {
                    final int rem_part = 64 - part;
                    
                    final long first_part = pref[rem_part];
                    final long last_part = suff[part];
                    
                    for (int i = 0; i < len - full; i++)
                        has_odd_reprs[i + full] ^= ((is_on[i] & first_part) << part);
                    for (int i = 0; i < len - full - 1; i++)
                        has_odd_reprs[i + full + 1] ^= ((is_on[i] & last_part) >>> rem_part);
                }
            }
        }
        
        return cnt;
    }
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int s = scanner.nextInt();
        System.out.println(solve(n, s));
        scanner.close();
    }
}
