#include<bits/stdc++.h>
using namespace std;
bool judge(int x,int y){
while(x!=y){
x%=y;
y%=x;
}
if(x!=1)return 1;
else return 0;
}
int ans=0,c[100001],j;
int main(){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
if(judge(i,n)==1){
ans++;
j++;
c[j]=i;
}
cout<<c[k%ans]+n*(k/ans);
return 0;
}