
In a distant kingdom, there lived Zmiy Horynych the Dragon, who had heads and tails.
Dobrynya Mykytych decided to defeat this dragon that threatened humanity. To help him, he was given a magical sword—the only weapon capable of slaying Zmiy Horynych the Dragon.
The sword can be used according to the following rules:
If one head is cut off, a new head immediately grows in its place.
If one tail is cut off, two new tails immediately grow in its place.
If two tails are cut off at once, one new head immediately grows in their place.
If two heads are cut off at once, nothing grows back.
The dragon can be slain only if all his heads and all his tails have been removed.
Determine the minimal number of sword strikes required to slay Zmiy Horynych the Dragon or establish that it is impossible.
Two integers , (0 \leq N,\ M \leq 1000).
A single integer—the minimal number of sword strikes needed to slay the dragon, or -1 if it is impossible.
Example 1