cswork / OAOpen practice workspace

Pure StorageOA #6

Lock Acquire/Release Log Checker

Estimated EasyJudge-ready

Topics (AI-labelled)Stacks & HeapsHashing

Each event is ACQUIRE lock or RELEASE lock. Locks must be released in the reverse order of acquisition; a lock that is already held cannot be acquired again, and a lock that is not held cannot be released. Return the 1-based index of the first erroneous event; if no event is erroneous but some lock is still held at the end, return the number of events + 1; otherwise return 0.

Input

The first line contains n; each of the next n lines is ACQUIRE name or RELEASE name. Added by this site: 0≤n≤200000, and name is an ASCII identifier without whitespace.

Output

Output the index of the first error, n+1 in the dangling case, or 0.

Example 1

Input:

4
ACQUIRE A
ACQUIRE B
RELEASE B
RELEASE A

Output:

0

Example 2

Input:

2
ACQUIRE A
RELEASE B

Output:

2

Example 3

Input:

3
ACQUIRE A
ACQUIRE B
RELEASE A

Output:

3

Editorial

Loading solution…