#include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } int main(){ ll n, a, b, c; cin >> n >> a >> b >> c; vector dp(2 * n, 1e18); dp[1] = 0; vector ikeru(2 * n, vector>(0)); vector costb(1, 1); while(true){ if (costb.back() * b >= 1e11) break; costb.push_back(costb.back() * b); } ll kaijo = 1; for (ll i=1; i<2*n; i++){ kaijo *= i; if (kaijo >= n) kaijo = kaijo % n + n; ikeru[i].push_back(pair(kaijo, c)); ll tar = i * i; if (tar >= n) tar = tar % n + n; rep(k,2,(int)costb.size()){ ikeru[i].push_back(pair(tar, costb[k])); tar *= i; if (tar >= n) tar = tar % n + n; } tar = i + 1; if (tar >= n) tar = tar % n + n; ikeru[i].push_back(pair(tar, a)); } priority_queue> pq; pq.push(pair(0, 1)); while(!pq.empty()){ auto [tmp, i] = pq.top(); pq.pop(); tmp = -tmp; if (tmp > dp[i]) continue; for (auto [j, cost]: ikeru[i]){ if (chmin(dp[j], cost + tmp)){ pq.push(pair(-dp[j], j)); } } } cout << dp[n] << '\n'; }