400. To and Fro

http://www.spoj.com/problems/TOANDFRO/