Roses

Time limit1sMemory limit128 MB

Summary
Given two bundle prices for roses, find the minimum cost to buy at least N roses using any combination of bundles from either shop.
Level

Medium6 of 10

Topics
Math, Greedy, Brute force
Solved
No attempts yet

Problem

To celebrate Valentine's Day, Sanggeun wants to give his girlfriend NN yellow roses. There are two flower shops near his house, and both have prepared plenty of flowers for Valentine's Day, so roses will never run out. However, both shops sell roses only in bundles.

The first shop sells AA roses for BB won, and the second shop sells CC roses for DD won. AA, BB, CC, and DD are all positive integers. If buying more than NN roses is cheaper than buying exactly NN, he can buy more and give the leftover roses to the shop clerk.

Write a program that computes the minimum amount of money Sanggeun needs to buy at least NN roses.

Input

The first line contains NN, AA, BB, CC, and DD, separated by spaces. NN does not exceed 101510^{15}, and AA, BB, CC, and DD do not exceed 10510^{5}.

Output

Print the minimum amount of money needed to buy at least NN roses. The answer never exceeds 101810^{18}.

Hint

In the first example, buying one bundle from the first shop (2 roses, 3 won) and two bundles from the second shop (20 roses, 28 won) yields 22 roses for 31 won.

Examples4

  1. Example 1

    Input
    22 2 3 10 14
    
    Expected output
    31
    
  2. Example 2

    Input
    1 1 1 1 1
    
    Expected output
    1
    
  3. Example 3

    Input
    3 5 1 1 1
    
    Expected output
    1
    
  4. Example 4

    Input
    7 3 5 3 4
    
    Expected output
    12