Catalan numbers - PANSTACK

https://www.codechef.com/APRIL12/problems/PANSTACK/
I came across this question on CP-Algorithms under Catalan Numbers. Can someone suggest me a way how this question can be done using the concept of Catalan numbers?