In Hong Kong, the Chinese (Lunar) New Year is widely celebrated, a time of vibrant traditions and joyous reunions. One of the most cherished customs is the giving of red packets, or "lai see" (利是), filled with money, symbolizing prosperity for the year ahead.
Red Packets (© e-print HK) Ah Ma, the beloved matriarch of the family, takes this tradition very seriously. This year, she has a total of $$$N$$$ one-dollar coins, to distribute among $$$M$$$ red packets for her numerous grandchildren, nieces, and nephews.
However, Ah Ma is a firm believer in numerology and the auspicious power of certain digits.
In particular, the digits 2, 6, and 8 are considered especially lucky, bringing fortune and happiness:
On the other hand, the digits 1, 3, 4, 5, 7, and 9 are seen as unlucky, associated with misfortune:
The digit 0 is neutral; considered neither lucky nor unlucky.
Therefore, Ah Ma insists that no red packet should contain an amount ending in an unlucky digit (1, 3, 4, 5, 7, or 9) (e.g. 4, 14, 23, 1, 59). While adhering strictly to this rule, she wants to maximize the number of red packets containing an amount ending in a lucky digit (2, 6, or 8) (e.g. 22, 48, 1345796, 444444442). Each red packet must also contain a positive amount, as an empty red packet is considered a bad omen.
With the Lunar New Year approaching in just over a week, Ah Ma needs your help. Given the total amount of one-dollar coins $$$N$$$ and the number of red packets $$$M$$$, can you determine if it's possible to distribute all the one-dollar coins, not a single coin less, according to her rules? And if so, what's the maximum number of red packets containing an amount ending in a lucky digit?
The first and only line of input consists of two integers $$$N$$$ and $$$M$$$:
For all cases:
If it is not possible to distribute the money such that no red packet contains an amount ending in an unlucky digit, output -1.
Otherwise, output the maximum number of red packets that can contain an amount ending in a lucky digit while adhering to the above rule.
8 4
4
24 5
5
Sample 1: Ah Ma should place 2 one-dollar coins into each red packet. All of them are lucky packets.
Sample 2: Ah Ma should place 2, 6, 8, 6 and 2 one-dollar coins into the red packets respectively. All of them are lucky packets.