Correcting Cheeseburgers

No attempts yetTime limit2sMemory limit512 MB

Problem

Cheeseburgers are serious business. They are the most delicious food on earth, but there is a lot of room for error when making one. Even otherwise capable cooks often mess up the order of the assembled ingredients.

The only correct order of ingredients between the buns is, from top to bottom:

  1. Ketchup and mustard
  2. Beef tomato
  3. Pickles
  4. Red onions
  5. Cheddar cheese
  6. Garlic
  7. Salt and pepper
  8. Beef patty, medium grilled
  9. Corn salad
  10. Mayonnaise

Any deviation from this order is completely unacceptable, so a cheeseburger sometimes has to be reassembled.

Space on an average plate and social norms are rather restrictive when it comes to operating on a cheeseburger. The only feasible operation is the bit-shuffle (burger-ineptly-transformed). A bit-shuffle separates the entire burger into four parts of contiguous ingredients aa, bb, cc, dd and reassembles them in the order cc, aa, dd, bb. You pick the size of each of the four parts, and a part may be empty.

Since the burger cools rapidly, find the minimum number of bit-shuffles needed to arrive at an acceptable burger.

Each given cheeseburger consists of nn unique ingredients labeled from 11 to nn. The correct order is always the natural order 1,2,,n1, 2, \dots, n.

Figure 1: an illustration of the first sample input.

Figure 2: an illustration of the second sample input.

Input

The first line contains one integer nn, the number of ingredients used (1n101 \le n \le 10).

The second line contains nn integers describing the order of the ingredients of the given cheeseburger, from top to bottom. The ingredients are numbered from 11 to nn, and every number appears exactly once.

Output

Output the minimum number of bit-shuffles needed to correct the given cheeseburger.