#agc061e. [agc061_e]Increment or XOR
[agc061_e]Increment or XOR
Problem Statement
There is a non-negative integer initially equal to . One can perform the following operations, in any order, any number of times:
- Add to . This has a cost of .
- Choose between and , and replace with . This has a cost of . Here, denotes bitwise .
Find the smallest total cost to make equal to a given non-negative integer , or report that this is impossible.
What is bitwise ?
The bitwise of non-negative integers and , , is defined as follows:
- When is written in base two, the digit in the 's place () is if exactly one of the digits in that place of and is , and otherwise.
For example, we have (in base two: ).
Generally, the bitwise of non-negative integers is defined as $(\\dots ((p_1 \\oplus p_2) \\oplus p_3) \\oplus \\dots \\oplus p_k)$. We can prove that this value does not depend on the order of .
Constraints
- ()
- ()
- All values in the input are integers.
Input
Input is given from Standard Input in the following format:
Output
If the task is impossible, print -1
. Otherwise, print the smallest total cost.
Sample Input 1
1 15 0 1
8 2
Sample Output 1
5
You can make equal to in the following manner:
- Choose and replace with to get . This has a cost of .
- Add to to get . This has a cost of .
- Choose and replace with to get . This has a cost of .
Sample Input 2
3 21 10 100
30 1
12 1
13 1
Sample Output 2
3
Sample Input 3
1 2 0 1
1 1
Sample Output 3
-1
Sample Input 4
8 352217 670575 84912
239445 2866
537211 16
21812 6904
50574 8842
380870 5047
475646 8924
188204 2273
429397 4854
Sample Output 4
563645