/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━━━━━━━━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 1000005
#define nm 200005
#define N 40005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const int inf=1e9;
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m,c[NM],tot,phi[NM],prime[NM],_x,cnt,s;
bool v[NM];
ll ans;
void Euler(){
n=1e6;phi[1]=1;
inc(i,2,n){
if(!v[i])prime[++tot]=i,phi[i]=i-1;
inc(j,1,tot){
if(i*prime[j]>n)break;
v[i*prime[j]]++;
if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
void dfs(int i,int s,int t){
if(s>_x)return;
if(i==tot+1){if(s>1)cnt+=_x/s*(t&1?1:-1);return;}
dfs(i+1,s,t);dfs(i+1,s*c[i],t+1);
}
bool check(int x){
cnt=_x=x;dfs(1,1,1);
return cnt<=m;
}
int main(){
Euler();
while(~scanf("%d%d",&n,&m)){
ans=(ll)m/phi[n]*n;m%=phi[n];
if(m==0)ans-=n,m=phi[n];
int t=n;tot=0;
inc(i,2,sqrt(t))if(t%i==0){
c[++tot]=i;
while(t%i==0)t/=i;
}
if(t>1)c[++tot]=t;
for(int x=1,y=n;x<=y;)
if(check(mid))s=mid,x=mid+1;else y=mid-1;
while(__gcd(s,n)>1)s--;
ans+=s;
printf("%lld\n",ans);
}
return 0;
}