def parse(inFile):
    words = inFile.getWords()[1:]
    return [(words[2*i],int(words[2*i+1])) for i in xrange(len(words)/2)]

def solve(data):
    pos = {'O':1,'B':1}
    time = {'O':0,'B':0}
    other = {'O':'B','B':'O'}
    for datum in data:
        [robot, button] = datum
        time[robot] = time[robot] + 1 + abs(button - pos[robot])
        pos[robot] = button
        if (time[robot] <= time[other[robot]]):
            time[robot] = time[other[robot]] + 1
    return max(time.values())

if __name__ == "__main__":
    from GCJ import GCJ
    GCJ(parse, solve, "/Users/lpebody/gcj/2011_q/", "a").run()

            
