#include using namespace std; using ll = long long; #define FOR(i, l, r) for(ll i = l; i < r; i++) #define rep(i, n) FOR(i, 0, n) #define FORR(i, l, r) for(ll i = r - 1; i >= l; i--) #define ALL(ar) begin(ar), end(ar) template inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const pair &p) { os << p.first << ' ' << p.second; return os; } template istream &operator>>(istream &is, vector &v) { for(T &x : v) is >> x; return is; } template ostream &operator<<(ostream &os, const vector &v) { for(size_t i = 0; i < v.size(); i++) os << (i ? " " : "") << v[i]; return os; } //------------------------------------------------- void solve() { ll n, a, b, c; cin >> n >> a >> b >> c; vector dp(n + 1, 1LL << 55); dp[1] = 0; ll tmp = 1; FOR(i, 1, n) { tmp *= i; if(tmp > n) { tmp = gcd(tmp, n); chmin(dp[n], dp[i] + c + c); } chmin(dp[tmp], dp[i] + c); chmin(dp[i + 1], dp[i] + a); ll cost = b; ll nxt = i; while(true) { chmin(dp[nxt], dp[i] + cost); ll now = nxt; nxt *= i; if(nxt > n) { chmin(dp[n], dp[i] + cost * b + c); nxt = gcd(nxt, n); } if(nxt == now) break; cost *= b; if(cost > dp[n]) break; } } // cout << dp << endl; cout << dp.back() << endl; } int main() { int t = 1; // cin >> t; while(t--) solve(); }