A Gift for the Lady
Kozak Vus spent a long time deciding on the perfect birthday gift for Lady. He wanted it to be both unique and practical, so he gave her a collection of number pairs , with a total of pairs.
Initially, Lady was puzzled by this unusual gift. The original set didn't quite appeal to her, so she decided to select a subset of these pairs that would meet specific criteria.
Lady aims to form a new set, which is a subset of the original one. Formally, this subset consists of pairs , where and for all , it holds that . Lady can choose anywhere from to all pairs.
Lady considers a set to be the most beautiful if it satisfies the following conditions:
1. Lady's favorite number is . Therefore, the bitwise OR of all selected for must not exceed . 2. The sum of all for should be maximized.
The bitwise OR operation (denoted as ) is performed on each bit of the numbers, resulting in if both bits are , and otherwise. For example, consider the numbers and . Their binary forms are and . Applying the OR operation bit by bit, the result is , which equals in decimal.
Lady is struggling to find the most beautiful set. She realizes this is a challenging task, so she only asks you to determine the maximum possible sum of all in the most beautiful set.
Input
The first line contains two integers and (, ).
Each of the next lines contains two integers and ().
Output
Output a single number — the maximum possible sum of the selected .
Examples
Note
In the first example, selecting the first and second pairs results in a bitwise OR of , with a sum of . It's impossible to increase the sum because including all three pairs results in a bitwise OR of , which exceeds .
In the second example, the optimal choice is the second, third, and fourth pairs, yielding a sum of . Including the first pair would limit the selection to only the third pair to keep the bitwise OR within . Thus, the maximum possible sum is .
Scoring
( points): ;
( points): , ;
( points): , where ;
( points): no additional restrictions.