Domain: Algorithms & Data Structures

You are given a permutation `p` of `{1, 2, ..., n}`. It is **not** sorted in ascending order and **not** sorted in descending order.

Initially, all elements are colored **red**. Players alternate turns. On each turn, a player may do one of:

1. **Rearrange** the permutation so that all **red** elements keep their positions (blue elements may be permuted arbitrarily).
2. Change the color of **one** red element to **blue**.
3. Skip the turn.

The **first** player wins if the permutation becomes `[1, 2, ..., n]`.
The **second** player wins if the permutation becomes `[n, n-1, ..., 1]`.
If the game lasts `100500` turns without a winner, it is a draw.

Determine the result under optimal play:
- output **1** if the first player wins,
- output **2** if the second player wins,
- output **0** if it is a draw.

### Input
- `n` (`3 ≤ n ≤ 5·10^5`)
- `p_1, ..., p_n`: a permutation of `1..n`, not fully ascending/descending

### Output
One integer: `1`, `2`, or `0`.

Grading rubric:
criterion: Does the answer state that using the “rearrange while keeping red positions fixed” operation is only useful if it produces an immediate win on that same move (because the opponent can undo any non-winning rearrangement next move)?
weight: 10
criterion: Does the answer define the set of indices needed for the first player (ascending) as S_asc = { i | p[i] ≠ i}?
weight: 10
criterion: Does the answer define the set of indices needed for the second player (descending) as S_desc = { i | p[i] ≠ n−i+1}?
weight: 10
criterion: Does the answer define a as the count of indices only the first player needs to make blue, i.e., a = |S_asc \\ S_desc|?
weight: 10
criterion: Does the answer define b as the count of indices only the second player needs to make blue, i.e., b = |S_desc \\ S_asc|?
weight: 10
criterion: Does the answer define c as the count of indices both players need to make blue, i.e., c = |S_asc ∩ S_desc|?
weight: 10
criterion: Does the answer state the first player winning condition as a + c ≤ b?
weight: 20
criterion: Does the answer state the second player winning condition as b + c < a?
weight: 10
criterion: Does the answer state the final decision rule: output 1 if (a+c ≤ b), else output 2 if (b+c < a), otherwise output 0?
weight: 10

Answer:
### Expert Solution

A “rearrange (keeping red positions fixed)” move is only useful if it produces an **immediate win** on that move; otherwise the opponent can undo any non-winning rearrangement on their next move because the set of fixed (red) positions hasn’t changed.

So the game reduces to a race to recolor enough positions to **blue** so that a winning permutation can be formed by permuting only blue positions.

---

## Required-blue index sets

- To reach ascending `[1, 2, ..., n]`, any index `i` with `p_i ≠ i` must become blue (a red mismatch can never be fixed):
  `S_asc = { i | p_i ≠ i }`

- To reach descending `[n, n-1, ..., 1]`, any index `i` with `p_i ≠ (n - i + 1)` must become blue:
  `S_desc = { i | p_i ≠ (n - i + 1) }`

Partition into counts:
- `a = |S_asc \ S_desc|`
- `b = |S_desc \ S_asc|`
- `c = |S_asc ∩ S_desc|`

---

## Optimal-play outcome

- First player wins iff:
  `a + c ≤ b`

- Second player wins iff:
  `b + c < a`

- Otherwise neither can force a win, so the result is a draw.

**Output rule:**
- if `a + c ≤ b` output **1**
- else if `b + c < a` output **2**
- else output **0**

All counts are computed in one pass over `i = 1..n` in `O(n)`.