The Ashmere Adventurers' Guild keeps its whole treasury in one chest holding exactly T gold coins, and tonight the guildmaster must pay out every single coin to the guild's n active adventurers. Guild law is strict: each adventurer receives a whole number of coins, each adventurer receives at least 1 coin, and no adventurer may ever be handed exactly 4 coins, since local superstition holds that four coins bring ruin on whoever carries them. An adventurer paid exactly 8 coins, however, is considered blessed for the whole month ahead. The guildmaster wants a lawful payout of the entire chest that maximizes the number of blessed adventurers.
Determine the largest possible number of adventurers who can be paid exactly 8 coins under some lawful payout that uses all T coins, or report that no lawful payout of the entire chest is possible at all.
A single line containing two integers T and n.
Print a single integer: the maximum number of adventurers who can receive exactly 8 coins under a lawful payout of the whole chest, or -1 if no lawful payout exists.
Example 1
Input
20 3
Expected
1
Explanation
With 3 adventurers and 20 coins, blessing two adventurers with 8 coins each would spend 16 coins and force the last adventurer to take the cursed 4 coins, which is forbidden. Blessing only one adventurer works: pay one adventurer 8 coins and split the remaining 12 coins between the other two so neither gets 4 (for example 6 and 6). No lawful payout can bless more than 1 adventurer, so the answer is 1.
Example 2
Input
16 2
Expected
2
Explanation
With exactly 16 coins for 2 adventurers, paying both adventurers exactly 8 coins uses all 16 coins, satisfies the at-least-1 rule, and neither receives the cursed 4, so both can be blessed. The answer is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →