Codeforces : Theatre Square

Problem Link : http://codeforces.com/problemset/problem/1/A

Solution :