class Solution(object):
    def check(self, events):
        if not events:
            return 0

        step, st = 1, []

        for e in events:
            a, b = e.split()
            if a == "ACQUIRE":
                # if accquire an already accquired lock
                if b in st:
                    return step
                st.append(b)
            else:
                # release an un accquired lock
                if not st or st[-1] != b:
                    return step
                st.pop()

            step += 1

        if st:  # still some lock is accquired not released:
            return len(events) + 1

        return 0


if __name__ == '__main__':
    # evts = ["ACQUIRE 1", "ACQUIRE 2", "RELEASE 2", "RELEASE 1"]
    # print Solution().check(evts)

    # evts = ["ACQUIRE 1", "ACQUIRE 2", "RELEASE 1", "RELEASE 2"]
    # print Solution().check(evts)

    # evts = ["ACQUIRE 1", "ACQUIRE 2", "ACQUIRE 3","RELEASE 3", "RELEASE 2", "ACQUIRE 4"]
    # print Solution().check(evts)

    evts = ["ACQUIRE 1", "ACQUIRE 2", "ACQUIRE 3", "RELEASE 3", "RELEASE 4", "ACQUIRE 4"]
    print Solution().check(evts)

    evts = ["ACQUIRE 1", "ACQUIRE 2", "ACQUIRE 3", "RELEASE 3", "ACQUIRE 2", "ACQUIRE 4"]
    print Solution().check(evts)
