Conceptually, this challenge is really simple. You're given a list of non-negative integers `ai`. If possible, find a non-negative integer `N`, such that the list consisting of `bi = ai XOR N` is sorted. If no such `N` exists, output should be anything that can't be mistaken for a valid `N`, e.g. a negative number, nothing at all, an error etc.

Here is an example:

```
[4, 7, 6, 1, 0, 3]

```

If we take every element in this list `XOR 5`, we get

```
[1, 2, 3, 4, 5, 6]

```

which is sorted. (Note that it is not a requirement for the resulting list to have unique elements and contain no gaps. If the result of such an operation was `[0, 1, 1, 3]` that would still be valid.) On the other hand for the list

```
[4, 7, 1, 6, 0, 3]

```

no such `N` exists.

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.

Input may be in any convenient list or string format. You may assume that the `ai` are less than `231` each and that the list contains at least one element.

Your code must handle any of the test cases (especially the four large ones) in a matter of a few seconds.

Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply.

### Test Cases

For every test case which doesn't return `-1` there's an infinite number of correct answers. The one listed here is the smallest one. Additional solutions exist by additionally setting bits which are the same across all integers in the input (notably those which are larger than the most-significant bit in the largest number of the list).

```
[4 7 6 1 0 3] => 5
[4 7 1 6 0 3] => -1
[0 1 3 4 6 7] => 0
[4 2 3 1] => 6
[2 3 0 0 7 7 4 5 11 11] => 2
[2 3 0 0 7 7 5 4 11 11] => -1
[1086101479 748947367 1767817317 656404978 1818793883 1143500039] => -1
[180522983 1885393660 751646477 367706848 331742205 724919510 850844696 2121330641 869882699 1831158987 542636180 1117249765 823387844 731663826 1762069894 240170102 1020696223 1212052937 2041219958 712044033 195249879 1871889904 1787674355 1849980586 1308879787 1743053674 1496763661 607071669 1987302942 178202560 1666170841 1035995406 75303032 1755269469 200581873 500680130 561748675 1749521426 1828237297 835004548 934883150 38711700 1978960635 209243689 1355970350 546308601 590319412 959613996 1956169400 140411967 112601925 88760619 1977727497 672943813 909069787 318174568 385280382 370710480 809689639 557034312 865578556 217468424 346250334 388513751 717158057 941441272 437016122 196344643 379529969 821549457 97008503 872313181 2105942402 603939495 143590999 1580192283 177939344 853074291 1288703007 1605552664 162070930 1325694479 850975127 681702163 1432762307 1994488829 780869518 4379756 602743458 1963508385 2115219284 1219523498 559301490 4191682 1918142271 169309431 346461371 1619467789 1521741606 1881525154] => -1
[37580156 64423492 87193676 91914964 93632157 96332899 154427982 176139560 184435039 228963836 230164674 279802291 301492375 309127664 345705721 370150824 380319820 403997410 410504675 416543032 418193132 424733526 428149607 435596038 477224208 515649925 519407995 525469350 614538124 624884850 642649261 653488151 679260270 685637235 690613185 739141066 825795124 832026691 832633584 833213619 852655299 913744258 917674993 921902522 925691996 931307936 954676047 972992595 997654606 1020009811 1027484648 1052748108 1071580605 1108881241 1113730139 1122392118 1154042251 1170901568 1180031842 1180186856 1206428383 1214066097 1242934611 1243983997 1244736049 1262979035 1312007069 1312030297 1356274316 1368442960 1377432523 1415342434 1471294243 1529353536 1537868913 1566069818 1610578189 1612277199 1613646498 1639183592 1668015280 1764022840 1784234921 1786654280 1835593744 1849372222 1875931624 1877593764 1899940939 2007896363 2023046907 2030492562 2032619034 2085680072 2085750388 2110824853 2123924948 2131327206 2134927760 2136423634] => 0
[1922985547 1934203179 1883318806 1910889055 1983590560 1965316186 2059139291 2075108931 2067514794 2117429526 2140519185 1659645051 1676816799 1611982084 1736461223 1810643297 1753583499 1767991311 1819386745 1355466982 1349603237 1360540003 1453750157 1461849199 1439893078 1432297529 1431882086 1427078318 1487887679 1484011617 1476718655 1509845392 1496496626 1583530675 1579588643 1609495371 1559139172 1554135669 1549766410 1566844751 1562161307 1561938937 1123551908 1086169529 1093103602 1202377124 1193780708 1148229310 1144649241 1257633250 1247607861 1241535002 1262624219 1288523504 1299222235 840314050 909401445 926048886 886867060 873099939 979662326 963003815 1012918112 1034467235 1026553732 568519178 650996158 647728822 616596108 617472393 614787483 604041145 633043809 678181561 698401105 776651230 325294125 271242551 291800692 389634988 346041163 344959554 345547011 342290228 354762650 442183586 467158857 412090528 532898841 534371187 32464799 21286066 109721665 127458375 192166356 146495963 142507512 167676030 236532616 262832772] => 1927544832
[1922985547 1934203179 1883318806 1910889055 1983590560 1965316186 2059139291 2075108931 2067514794 2117429526 2140519185 1659645051 1676816799 1611982084 1736461223 1810643297 1753583499 1767991311 1819386745 1355466982 1349603237 1360540003 1453750157 1461849199 1439893078 1432297529 1431882086 1427078318 1487887679 1484011617 1476718655 1509845392 1496496626 1583530675 1579588643 1609495371 1559139172 1554135669 1549766410 1566844751 1562161307 1561938937 1123551908 1086169529 1093103602 1202377124 1193780708 1148229310 1144649241 1257633250 1241535002 1247607861 1262624219 1288523504 1299222235 840314050 909401445 926048886 886867060 873099939 979662326 963003815 1012918112 1034467235 1026553732 568519178 650996158 647728822 616596108 617472393 614787483 604041145 633043809 678181561 698401105 776651230 325294125 271242551 291800692 389634988 346041163 344959554 345547011 342290228 354762650 442183586 467158857 412090528 532898841 534371187 32464799 21286066 109721665 127458375 192166356 146495963 142507512 167676030 236532616 262832772] => -1

```

Finally, here are four very large test cases to ensure that submissions are sufficiently efficient:

* [Test Case 1](http://pastebin.com/P96PNi79) should yield `-1`.
* [Test Case 2](http://pastebin.com/zCNLMsx9) should yield `0`.
* [Test Case 3](http://pastebin.com/GFLBXn5b) should yield `1096442624`.
* [Test Case 4](http://pastebin.com/6F1Yn3gG) should yield `-1`.

### Why would anyone do this?

It occurred to me the other day that a XOR operation can "sort" an array, which makes it possible to perform a binary search on the array in *O(log n)* without having to sort it first. It appears to be possible to determine `N` in pseudolinear time, which would make this a faster alternative to most sorting algorithms, and it doesn't have the memory requirements of radix sort. Of course, a straight linear search through the unsorted array will be faster, but if you want to search the same array many times, a single linear pre-computation can bring down the time required for each search significantly.

Unfortunately, the class of lists this works on is rather limited (uniformly random distributions are unlikely to admit an `N`).

An interesting question is whether there are other bijective functions which are easier to check and/or are applicable for a wider class of lists.