#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long N,A,B,C; cin >> N >> A >> B >> C; long long Limit = N; vector> fac(Limit+1,{1,false}); for(long long i=1; i<=Limit; i++){ fac.at(i) = fac.at(i-1); fac.at(i).first *= i; if(fac.at(i).first >= N) fac.at(i).first %= N,fac.at(i).second = true; } long long inf = 1e18; vector al1(N),al2(N); vector M1(N,inf),M2(N,inf); priority_queue,vector>,greater<>> Q; M1.at(1) = 0; Q.push({0,false,1}); while(Q.size()){ auto[nowc,Over,v] = Q.top(); Q.pop(); if(v == 0){cout << nowc << endl; break;} if(!Over){ if(al1.at(v)) continue; al1.at(v) = true; if(M1.at((v+1)%N) > nowc+A) M1.at((v+1)%N) = nowc+A,Q.push({nowc+A,Over,(v+1)%N}); long long cost = B,next = v,over = 0; while(cost*B <= 1e11){ next *= v; cost *= B; if(next >= N) over = 1; next %= N; if(over){if(M2.at(next) > nowc+cost) M2.at(next) = nowc+cost,Q.push({nowc+cost,true,next});} else{if(M1.at(next) > nowc+cost) M1.at(next) = nowc+cost,Q.push({nowc+cost,false,next});} } { auto[next,over] = fac.at(v); cost = C; if(over){if(M2.at(next) > nowc+cost) M2.at(next) = nowc+cost,Q.push({nowc+cost,true,next});} else{if(M1.at(next) > nowc+cost) M1.at(next) = nowc+cost,Q.push({nowc+cost,false,next});} } } else{ if(al2.at(v)) continue; al2.at(v) = true; if(M2.at((v+1)%N) > nowc+A) M2.at((v+1)%N) = nowc+A,Q.push({nowc+A,Over,(v+1)%N}); long long cost = B,next = v,over = 1; while(cost*B <= 1e11){ next *= v; cost *= B; if(next >= N) over = 1; next %= N; if(over){if(M2.at(next) > nowc+cost) M2.at(next) = nowc+cost,Q.push({nowc+cost,true,next});} else{if(M1.at(next) > nowc+cost) M1.at(next) = nowc+cost,Q.push({nowc+cost,false,next});} } { if(M2.at(0) > nowc+C) M2.at(0) = nowc+C,Q.push({nowc+C,true,0}); } } } }