Jiyong is a baker who is good at making pancakes. One day he baked N pancakes, all of different sizes, each with a front side and a back side you can tell apart. Listed from smallest to largest, their sizes are 1,2,…,N. Jiyong stacked them in the reverse of the order he baked them without thinking about it, so the stack may not be sorted by size. He wants to use the smallest number of flips to reach a stack whose sizes decrease toward the top and whose pancakes all have the front side up.
A flip works like this. Pick an i with 1≤i≤N, lift the top i pancakes together, and turn them over. The order of those i pancakes reverses and each of them changes the side that faces up. For example, if the stack from the top is 1(+) 2(+) 3(+) 4(+) 5(+) and you pick i=3, it becomes 3(-) 2(-) 1(-) 4(+) 5(+). Here + means the front side faces up and - means the back side faces up.
Help Jiyong and find the minimum number of flips he needs.