n,a,b,c = map(int, input().split()) g = [[] for _ in range(n*2)] g[n].append((0, 0)) fact = 1 fact_ = 0 for i in range(n): g[i].append((i+1, a)) if i < n-1: g[i+n].append((i+n+1, a)) else: g[i].append((n, a)) ok = 0 B = b**2 ik = i ** 2 if ik >= n: ok = 1 ik %= n for k in range(2, 30): if B >= 2 * 10**6: break if ok: g[i].append((ik+n, B)) g[i+n].append((ik+n, B)) else: g[i].append((ik, B)) g[i+n].append((ik+n, B)) if ik * i >= n: ok = 1 ik = ik * i % n else: ik *= i B *= b g[i+n].append((0, c)) if fact_: g[i].append((fact + n, c)) else: g[i].append((fact, c)) if fact * (i+1) >= n: fact_ = 1 fact = fact * (i+1) % n from heapq import * INF = 10**18 def dijkstra(g, s, n): dist = [INF]*n hq = [(0, s)] dist[s] = 0 while hq: d, v = heappop(hq) if dist[v]!=d: continue for to, c in g[v]: if dist[v] + c < dist[to]: dist[to] = dist[v] + c heappush(hq, (dist[to], to)) return dist dist = dijkstra(g, 1, n*2) print(dist[0])