Arkady owns a non-decreasing array $a_1, a_2, \ldots, a_n$. You are jealous of its beauty and want to destroy this property. You have a so-called XOR-gun that you can use one or more times.

In one step you can select two consecutive elements of the array, let's say $x$ and $y$, remove them from the array and insert the integer $x \oplus y$ on their place, where $\oplus$ denotes the bitwise XOR operation . Note that the length of the array decreases by one after the operation. You can't perform this operation when the length of the array reaches one.

For example, if the array is $[2, 5, 6, 8]$, you can select $5$ and $6$ and replace them with $5 \oplus 6 = 3$. The array becomes $[2, 3, 8]$.

You want the array no longer be non-decreasing. What is the minimum number of steps needed? If the array stays non-decreasing no matter what you do, print $-1$.


-----Input-----

The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the initial length of the array.

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array. It is guaranteed that $a_i \le a_{i + 1}$ for all $1 \le i < n$.


-----Output-----

Print a single integer — the minimum number of steps needed. If there is no solution, print $-1$.


-----Examples-----

Input
4
2 5 6 8
Output
1
Input
3
1 2 3
Output
-1
Input
5
1 2 4 6 20
Output
2


-----Note-----

In the first example you can select $2$ and $5$ and the array becomes $[7, 6, 8]$.

In the second example you can only obtain arrays $[1, 1]$, $[3, 3]$ and $[0]$ which are all non-decreasing.

In the third example you can select $1$ and $2$ and the array becomes $[3, 4, 6, 20]$. Then you can, for example, select $3$ and $4$ and the array becomes $[7, 6, 20]$, which is no longer non-decreasing.

Source: codeforces | Difficulty: HARD
Tags: Complete search

Bit manipulation

Constructive algorithms


Solutions:
inf = 10 ** 16
n = int(input())
aa = list(map(int, input().split()))
ans = inf
if n > 70:
	print(1)
	exit()
for i in range(1, n - 1):
	ll = []
	mx = -inf
	x = 0
	for j in range(i - 1, -1, -1):
		x ^= aa[j]
		if x > mx:
			mx = x
			ll.append((x, i - 1 - j))
	rr = []
	mn = inf
	x = 0
	for j in range(i, n):
		x ^= aa[j]
		if x < mn:
			mn = x
			rr.append((x, j - i))
	j = 0
	for (x, c) in ll[::-1]:
		while j < len(rr) and rr[j][0] >= x:
			j += 1
		if j == len(rr):
			break
		cur = c + rr[j][1]
		ans = min(ans, cur)
print(-1) if ans == inf else print(ans)


n = int(input())
a = list(map(int, input().split()))
res = -1
xor_a = [0]
for i in range(n):
	xor_a.append(a[i] ^ xor_a[-1])
(step, i) = (1, 0)
while step < n - 1:
	i = 0
	while i < n:
		for x in range(step + 1):
			if step + 1 + i - x + 1 <= n and i - x >= 0:
				if xor_a[i - x] ^ xor_a[i + 1] > xor_a[1 + i] ^ xor_a[1 + i + step - x + 1]:
					res = step
					i = n
					step = n
		i += 1
	step += 1
print(res)


import sys
import math, bisect, operator
(inf, m) = (float('inf'), 10 ** 9 + 7)
sys.setrecursionlimit(10 ** 6)
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, defaultdict
I = lambda : int(sys.stdin.readline())
neo = lambda : map(int, sys.stdin.readline().split())
Neo = lambda : list(map(int, sys.stdin.readline().split()))

def solve(case):
	n = I()
	a = Neo()
	pre = [0] * (n + 1)
	for i in range(n):
		pre[i] = pre[i - 1] ^ a[i]
	if n > 60:
		print(1)
		return
	ans = inf
	for i in range(n - 1):
		for j in range(i + 1, n):
			for k in range(i, j):
				left = pre[k] ^ pre[i - 1]
				right = pre[j] ^ pre[k]
				if right < left:
					ans = min(ans, j - i - 1)
	print(ans if ans != inf else -1)
solve(1)


import sys

def left(n):
	i = -1
	while n > 0:
		i += 1
		n //= 2
	return i
n = int(input())
arr = list(map(int, input().split()))
i1 = 0
while i1 + 2 < n:
	i2 = i1 + 1
	i3 = i1 + 2
	if left(arr[i1]) == left(arr[i2]) and left(arr[i1]) == left(arr[i3]):
		print(1)
		sys.exit()
	i1 += 1

def tong(l, r):
	res = 0
	for i in range(l, r + 1):
		res ^= arr[i]
	return res
kq = 10 ** 12
for a in range(n):
	for b in range(a, n):
		c = b + 1
		for d in range(c, n):
			t1 = tong(a, b)
			t2 = tong(c, d)
			if t1 > t2:
				kq = min(kq, b - a + 1 - 1 + (d - c + 1) - 1)
print(-1 if kq == 10 ** 12 else kq)


from sys import stdin
from collections import deque
import math

def mxb(n):
	k = int(math.log(n, 2))
	return 2 ** k
n = int(stdin.readline().strip())
s = list(map(int, stdin.readline().strip().split()))
mx = [mxb(i) for i in s]
for i in range(2, n):
	if mx[i] == mx[i - 1] and mx[i - 1] == mx[i - 2]:
		print(1)
		exit(0)
last = -100
ans = 100000000000
for i in range(n):
	if i > 0:
		last = s[i - 1]
	xor = s[i]
	for j in range(i + 1, n):
		xor = xor ^ s[j]
		if xor < last:
			ans = min(ans, j - i)
		if j != n - 1 and xor > s[j + 1]:
			ans = min(ans, j - i)
		if j == n - 1:
			continue
		c = 1
		xor1 = s[j + 1]
		for k in range(j + 2, n):
			xor1 = xor1 ^ s[k]
			if xor > xor1:
				ans = min(ans, j - i + c)
			c += 1
if ans != 100000000000:
	print(ans)
else:
	print(-1)


input()
v = list(map(lambda x: int(x), input().split()))

def ok(x):
	print(x)
	raise SystemExit
if len(v) == 2:
	ok(-1)

def absolute(x):
	q = 0
	while x != 0:
		x >>= 1
		q += 1
	return q

def xor(v, y, x):
	q = 0
	y = max(0, y)
	for i in range(y, x + 1):
		q ^= v[i]
	return q
for i in range(2, len(v)):
	if absolute(v[i - 2]) == absolute(v[i - 1]) == absolute(v[i]):
		ok(1)
q = 10000000000.0
for a in range(0, len(v) - 1):
	for b in range(a, len(v)):
		c = b + 1
		for d in range(c, len(v)):
			if xor(v, a, b) > xor(v, c, d):
				if q > b - a + (d - c):
					q = b - a + (d - c)
if q != 10000000000.0:
	ok(q)
else:
	ok(-1)