Problem : http://www.spoj.com/problems/TOANDFRO/Solution : http://ideone.com/BE1gcp
Stuck for a day on this problem
Solved THx