MAGNETSORT

can anyone tell me where is i am wrong
question link is Contest Page | CodeChef
my submission link is Solution: 59333177 | CodeChef
in my solution for ans=1,2 i take last pole which is different and check before this and after this is both side are not sorted then ans is 2 else ans is 1.