import heapq N,A,B,C=map(int,input().split()) fac=[1]*N fac_over=[0]*N for i in range(1,N): fac[i]=fac[i-1]*i fac_over[i]=fac_over[i-1] if fac[i]>=N: fac[i]%=N fac_over[i]=1 dist=[N*A]*(N+1) dist[1]=0 q=[(0,1)] while q: d,v=heapq.heappop(q) if v==0: break if dist[v]d+C: dist[0]=d+C heapq.heappush(q,(d+C,0)) continue # 操作1 if dist[(v+1)%N]>d+A: dist[(v+1)%N]=d+A heapq.heappush(q,(d+A,(v+1)%N)) # 操作2 b,v2=B,v ov=0 while d+bd+b: dist[N]=d+b heapq.heappush(q,(d+b,N)) if dist[v2]>d+b: dist[v2]=d+b heapq.heappush(q,(d+b,v2)) b*=B v2*=v if v2>=N: ov=1 v2%=N # 操作3 if fac_over[v] and dist[N]>d+C: dist[N]=d+C heapq.heappush(q,(d+C,N)) if dist[fac[v]]>d+C: dist[fac[v]]=d+C heapq.heappush(q,(d+C,fac[v])) print(dist[0])