import
java.io.*;
import
java.util.*;
class
GFG {
public
static
ArrayList<Integer> a
=
new
ArrayList<Integer>();
public
static
int
result =
0
;
public
static
boolean
is_sorted(ArrayList<Integer> a)
{
for
(
int
i =
1
; i < a.size(); i++) {
if
(a.get(i) < a.get(i -
1
))
return
false
;
}
return
true
;
}
public
static
boolean
mergeSubTrees(ArrayList<Integer> a,
int
low,
int
mid,
int
high)
{
if
(a.get(mid) < a.get(mid +
1
)) {
return
false
;
}
for
(
int
i = low; i <= mid; i++) {
int
tempswap = a.get(i);
a.set(i, a.get(i + (high - low +
1
) /
2
));
a.set(i + (high - low +
1
) /
2
, tempswap);
}
return
true
;
}
public
static
void
minSwapsRequired(ArrayList<Integer> a,
int
low,
int
high)
{
if
(low < high) {
int
mid = low + (high - low) /
2
;
minSwapsRequired(a, low, mid);
minSwapsRequired(a, mid +
1
, high);
if
(mergeSubTrees(a, low, mid, high) ==
true
) {
result = result +
1
;
}
}
}
public
static
void
main(String[] args)
{
int
N =
3
;
N = (
1
<< N);
a.add(
4
);
a.add(
3
);
a.add(
2
);
a.add(
1
);
a.add(
5
);
a.add(
6
);
a.add(
7
);
a.add(
8
);
minSwapsRequired(a,
0
, N -
1
);
if
(is_sorted(a))
System.out.println(result);
else
System.out.println(
"-1"
);
}
}