#include<bits/stdc++.h>
using namespace std;
long long a,b,c,d,ans=0;
int main()
{
cin>>a>>b>>c>>d;
if(a==b)
{
if(c==0)
{
int ok=1;
for(int j=2;j*j<=b;j++)
{
if(b%j==0)
{
ok=0;
break;
}
}
if(ok==1)cout<<b;
else cout<<-1;
}
else if(c>=1&&c<=b)
{
int ok=1;
for(int j=2;j*j<=c;j++)
{
if(c%j==0)
{
ok=0;
break;
}
}
if(ok==1)cout<<c;
else cout<<-1;
}
else cout<<-1;
return 0;
}
for(int i=(a-c)/b;i>=0;i--)
{
int ok=1;
for(int j=2;j*j<=i*b+c;j++)
{
if((i*b+c)%j==0)
{
ok=0;
break;
}
}
if(ok==0)continue;
else ans++;
if(ans==d){cout<<i*b+c;return 0;}
}
cout<<-1;
}
气死我了要是过了第一个点就AK入门赛了,求各路大佬帮我看看问题出在哪谢谢