def control (filename, hour):
    n = 0
    with open (filename, 'r') as f:
        l1 = f.readline()
        l2 = f.readline()
        for line in f:
            line = line.strip().split(', ')
            dni, name, time, action = line
            h, m = time.split(':')
            h = int(h)
            if h == hour and action == 'E':
                n = n + 1
            elif h >= hour+1:
                break
    return n


def add_time(h1, m1, h2, m2):
    mt = m1 + m2
    ht = h1 + h2 + mt//60
    mt = mt%60
    return ht, mt

def subtract_time(h1, m1, h2, m2):
    if m1 < m2:
        h1 = h1 - 1
        m1 = m1 + 60
    return h1-h2, m1-m2

def control_1 (filename, dni_given):
    ht, mt = 0, 0
    with open (filename, 'r') as f:
        l1 = f.readline()
        l2 = f.readline()
        for line in f:
            line = line.strip().split(', ')
            dni, name, time, action = line
            if dni == dni_given:
                h, m = time.split(':')
                if action == 'E':
                    he, me = int(h), int(m)
                else:
                    hs, ms = int(h), int(m)
                    hp, mp = subtract_time(hs, ms, he, me)
                    ht, mt = add_time(ht, mt, hp, mp)
    return ht, mt

def control_2 (filename, dni_given):
    d = {}
    with open (filename, 'r') as f:
        l1 = f.readline()
        l2 = f.readline()
        for line in f:
            line = line.strip().split(', ')
            dni, name, time, action = line
            if dni not in d:
                h, m = time.split(':')
                d[dni] = int(h)*60 + int(m)
        lt = sorted (d.items(), key = lambda x: (x[1], x[0]))
    return dni_given not in d or dni_given == lt[-1][0]

