Given a permutation $\pi:[n]\to[n]$ of cards from $1$ to $n$ you can find the first card to move to the front the following way:
- $c \leftarrow n$
- for each $k$ from $n$ down to $1$ do
- $\space\space\space$ if $c = \pi(k)$ then
- $\space\space\space\space\space\space$ $c \leftarrow c - 1$
- $\space\space\space$ end if
- end for
- return $c$