#include
#include
#include
#include
#include
#define LD double
#define LL long long
#define ULL unsigned long long
#define min(a,b) ((a
#define max(a,b) ((a>b)?a:b)
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
#define fr(i,j) for(int i=begin[j];i;i=next[i])
using namespace std;
int const mn=1e5+9,mm=2*1e5+9,lim=1e7,inf=1e9;
int n,p,ss[lim+9],tag[lim+9],mu[lim+9],mu2[lim+9],mu3[lim+9],hs[lim+9],a[lim+9],b[lim+9],c[lim+9];
int hash(int x){
int tmp=x%lim;
while(hs[tmp]&&(hs[tmp]!=x))tmp=(tmp+1==lim)?0:tmp+1;
return tmp;
}
int s(int x){
if(x<=lim)return mu[x];
int ans=1,tmp=hash(x);
if(hs[tmp]&&a[tmp])return a[tmp];
for(int i=2,j;i<=x;i=j+1){
j=x/(x/i);
ans=(ans-1ll*s(x/i)*(j-i+1))%p;
}
hs[tmp]=x;
return a[tmp]=ans;
}
int s2(int x){
if(x<=lim)return mu2[x];
int ans=1,tmp=hash(x);
if(hs[tmp]&&b[tmp])return b[tmp];
for(int i=2,j;i<=x;i=j+1){
j=x/(x/i);
ans=(ans-(1ll*(i+j)*(j-i+1)/2%p)*s2(x/i))%p;
}
hs[tmp]=x;
return b[tmp]=ans;
}
int f2(int x){
LL tmp=1ll*x*(x+1)/2;
if(tmp%3==0)return tmp/3%p*(2*x+1)%p;
else return 1ll*(2*x+1)/3*(tmp%p)%p;
}
int s3(int x){
if(x<=lim)return mu3[x];
int ans=1,tmp=hash(x);
if(hs[tmp]&&c[tmp])return c[tmp];
for(int i=2,j;i<=x;i=j+1){
j=x/(x/i);
ans=(ans-1ll*(f2(j)-f2(i-1))*s3(x/i))%p;
}
hs[tmp]=x;
return c[tmp]=ans;
}
int f(int x){
return (1ll*x*(x+1)/2%p)*(1ll*x*(x+1)/2%p)%p;
}
int main(){
//freopen("cake.in","r",stdin);
//freopen("cake.out","w",stdout);
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
scanf("%d%d",&n,&p);
mu[1]=1;
mu2[1]=1;
mu3[1]=1;
fo(i,2,lim){
if(!tag[i])ss[++ss[0]]=i,mu[i]=-1,mu2[i]=-i,mu3[i]=1ll*(-i)*i%p;
fo(j,1,ss[0]){
if(i*ss[j]>lim)break;
tag[i*ss[j]]=1;
mu[i*ss[j]]=-mu[i];
mu2[i*ss[j]]=1ll*(-mu2[i])*ss[j]%p;
mu3[i*ss[j]]=1ll*(-mu3[i])*ss[j]%p*ss[j]%p;
if(i%ss[j]==0){
mu[i*ss[j]]=0;
mu2[i*ss[j]]=0;
mu3[i*ss[j]]=0;
break;
}
}
mu[i]=(mu[i]+mu[i-1])%p;
mu2[i]=(mu2[i]+mu2[i-1])%p;
mu3[i]=(mu3[i]+mu3[i-1])%p;
}
int ans=0;
for(int i=1,j;i1 ){
j=(n-1)/((n-1)/i);
int tmp1=1ll*(s(j)-s(i-1))*((n-1)/i)%p*((n-1)/i)%p*n%p*n%p,
tmp2=1ll*(s2(j)-s2(i-1))*n%p*((n-1)/i)%p*((n-1)/i)%p*((n-1)/i+1)%p,
tmp3=1ll*(s3(j)-s3(i-1))*f((n-1)/i)%p%p;
ans=(ans+1ll*(s(j)-s(i-1))*((n-1)/i)%p*((n-1)/i)%p*n%p*n%p
-1ll*(s2(j)-s2(i-1))*n%p*((n-1)/i)%p*((n-1)/i)%p*((n-1)/i+1)%p
+1ll*(s3(j)-s3(i-1))*f((n-1)/i)%p)%p;
}
printf("%d",((ans*2+1ll*2*n*n-2*n)%p+p)%p);
return 0;
}