#include <cstdio>
#include <climits>
#include <cstring>
#define il inline
#define re register
#define int long long
#define gc() getchar()
#define pc(ch) putchar(ch)
#define max(a,b) (a) > (b) ? (a) : (b)
il int read(){
re char ch = gc();
re int d = 0,s = 1;
while (ch < '0' || ch > '9'){
if (ch == '-') s = -1;
ch = gc();
}
while (ch >= '0' && ch <= '9')
d = d * 10 + (ch - '0'),ch = gc();
return d * s;
}
il void read(re int &x){x = read();}
il void read(re int &x,re int &y){read(x),read(y);}
il void write(re int x){
if (x < 0) pc('-'),x = -x;
if (x > 9) write(x / 10);
pc(x % 10 + '0');
}
il void writeln(re int x){write(x),pc('\n');}
signed main(){
int n,x,bc = LLONG_MAX,bcc = LLONG_MAX,ans = LLONG_MIN,score[(int)1e6+5];
read(n,x);
memset(score,0,sizeof(score));
for (int i=1;i<=n;i++){
bcc = bc;
bc = x;
x = ((bc * 3703 + 1047) % n) + 1;
if (bc == x && bcc == x) score[x] += 3;
else if (bc == x) score[x] += 2;
else score[x]++;
}
for (int i=1;i<=n;i++) ans = max(ans,score[i]);
writeln(ans);
return 0;
}