import heapq inf = 1 << 63 def dijkstra(s, n, edge): dist = [inf]*n dist[s] = 0 hq = [[0,s]] heapq.heapify(hq) while len(hq) > 0: d,i = heapq.heappop(hq) if dist[i] < d: continue for j,d_1 in edge[i]: if dist[j] > (dist[i] + d_1): dist[j] = dist[i] + d_1 heapq.heappush(hq, [dist[j],j]) return dist N,A,B,C = map(int,input().split()) fact = [1] for i in range(1,N+1): f = fact[-1]*i f %= N fact.append(f) G = [[] for i in range(2*N)] for x in range(1,N): G[N+x].append((0,C)) G[N].append((0,0)) for x in range(1,2*N): G[x].append(((x+1) % (2*N),A)) for x in range(2,N): f = 0 xx = x b = B for k in range(2,22): xx *= x if f == 0: if xx >= N: f = 1 xx %= N else: xx %= N b *= B if b >= 2*A + 4*C: break if xx % N == 0: G[x].append((0,b)) G[x+N].append((0,b)) break if f: G[x].append((N+xx,b)) else: G[x].append((xx,b)) G[x+N].append((N + xx,b)) if fact[x] == 0: G[x].append((0,C)) continue if x >= 9: G[x].append((N + fact[x],C)) else: f = 1 for i in range(1,x+1): f *= i if f > N: G[x].append((N + fact[x],C)) else: G[x].append((fact[x],C)) dist = dijkstra(1, 2*N, G) print(dist[0])