QAQ
#include<bits/stdc++.h>
using namespace std;
long long a,b[100005],c,d=1,e=0,f,g;
int main(){
cin>>a>>b[1];
for(int i=2;i<=a;i++){
b[i]=(b[i-1]*37+33031)%a+1;
}
cout<<endl;
sort(b+1,b+a+1);
for(int i=1;i<=a;i++){
if(b[i]==b[i+1])d++;
else{
if(d>e){
e=d;
f=b[i];
g++;
d=1;
}
}
}
if(g!=0)cout<<f;
else cout<<b[1];
return 0;
}