#000288

BubbleTractor

In a galaxy far, far away, it is a period of civil war. Rebel spaceships, striking from a hidden base, have won their first victory against the evil Galactic Empire. However, the Empire stroke back and the main Rebel spaceship has been trapped by a positronic tractor beam. Therefore, the Rebels need an ionic tractor disruptor to set the main spaceship free. Trouble is, a regular ionic tractor disruptor, which they have plenty of, will not do. No, no, Rebels actually need a negative ionic tractor disruptor and there is only one in the whole galaxy. The problem is that the spaceship that carries the negative ionic tractor disruptor has a broken odometer and Rebels are not sure if they have enough fuel to get to the destination. So, they want to calculate the correct number of light-years they have traveled.


The odometer is a device made of gears that rotate along the same axis and every gear has digits attached to it (a consecutive array of digits from 0 to max_digit). When all current digits on every gear are looked at as one number, the odometer displays the number of light-years traveled as an integer. However, the odometer has a defect – one (same) digit is broken on all gears.

Image: Dial
In the normal case, the gear proceeds from digit d to digit d+1 or from digit d to digit 0 if d is the maximum digit on certain gear. But for broken digit, odometer skips that digit and proceeds to the following digit on the gear. This defect shows up in all positions. For example, if broken digit is 3, odometer displays 45229 and spaceship travels one light-year, odometer reading changes to 45240 (instead of 45230). The additional problem is that that maximum digit doesn’t have to be the same on all gears. You have information about what the maximum digit is for every gear (1 ≤ max_digiti ≤ 9). If the maximum digit on gear 0 is a, on gear 1 is b, on gear 2 is c, on gear 3 is d, then maximum 4-digit number on the odometer is dcba. Then, after the spaceship travels another light-year, the odometer reading changes to 10000.


Your task is to calculate what would the odometer reading be if no digit was broken. You are provided with the incorrect odometer reading, the broken digit and the odometer description (maximal digit per every gear). Note that the broken digit can be larger than some maximum digits. On these positions odometer works just fine. Also, if broken digit is 0, odometer does not make mistakes on leading zeros.


InputFirst line of input contains two numbers: a positive integer n with 1 ≤ n ≤ 2^63-1 which represents the odometer reading, and a broken digit b with 0 ≤ b ≤ 9. You may assume that the odometer reading will not contain the broken digit.
Second line contains 19 maximum digits from (least significant) position 0 to (most significant) position 18, with 1 ≤ max_digiti ≤ 9. Note that first maximal digit matches the right-most gear and so on.

OutputPrint the number of light-years that would be present on the odometer if no digit was broken.


Input:
45 3
8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 2 2 2

Output:
31


Input:
45 0
8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 2 2 2

Output:
41

Submit solution

Coming later

The grading service will be connected in a later migration step. You can inspect the task and your previous results now.