Hackerrank: magic portals

i am sorry to post a hackerrank question here but i believe that coding is free and no discrimination on it :slight_smile:

the link : Programming Problems and Competitions :: HackerRank
please help