changetime = 10

# A connection is a list of four items:
# [<start_station>, <dep_time>, <arr_time>, <end_station>]
# departure and arrival time <dep_time>, <arr_time>,
# are expressed in whole numbers of minutes, not strings

# The problem statement is at
# https://cw.fel.cvut.cz/wiki/courses/be5b33pge/practices/examexample1
# and also at the end of this file as a comment.

def tominstr( t ):
    s1 = str( t // 60)
    s2 = str( t % 60)
    if len(s1) == 1: s1 = "0" + s1
    if len(s2) == 1: s2 = "0" + s2
    return s1 + ":" +s2

def tomin( HM ):        # "06:08" --> whole minutes --> 368  (min)
    hm = HM.split(':')
    return int(hm[0])*60 + int(hm[1])


def time( t1, t2, t3, t4 ):
    global changetime
    if t3 - t2 < 10: return 10**11, 10**11
    return t4-t1, t4-t3 + t2-t1

# <start_station>, <dep_time>, <arr_time>, <end_station>]     conn1   A to B
# <start_station>, <dep_time>, <arr_time>, <end_station>]     conn2   B to C
#        0              1          2             3
def sol( src, trg, allconn):
    bestTwhole = 10**10    # T stands for time, initial value impossibly big
    bestTdrive = 10**10
    bestConn1, bestConn2  = None, None
    for conn1 in allconn:
        if conn1[0] != src: continue
        for conn2 in allconn:
            if conn2[0] != conn1[3] or conn2[3] != trg: continue
            Twhole, Tdrive = time( conn1[1], conn1[2], conn2[1], conn2[2] )
            if Twhole < bestTwhole:
                bestTwhole, bestTdrive = Twhole, Tdrive
                bestConn1, bestConn2  = conn1, conn2
                # printSol(conn1, conn2, Twhole, Tdrive )
                continue
            if Twhole == bestTwhole:
                if Tdrive < bestTdrive:
                    bestTdrive = Tdrive
                    bestConn1, bestConn2  = conn1, conn2
                    # printSol(conn1, conn2, Twhole, Tdrive )

    return  bestConn1, bestConn2, bestTwhole, bestTdrive

def printSol(con1, con2, t1min, t2min):
    print( "plain result" , con1, con2, t1min, t2min )
    print( con1[0], tominstr(con1[1]), tominstr(con1[2]), con1[3])
    print( con2[0], tominstr(con2[1]), tominstr(con2[2]), con2[3])
    print( tominstr(t1min) )
    print( tominstr(t2min) )


# g = [<start_station>, <dep_time>, <arr_time>, <end_station>]
def getallConn():
    allConn = []
    N = int(input())
    src = input()
    trg = input()
    for i in range(N):
        s = input().strip()
        #print(s)
        ss = s.split(":")  # ["Mossley West 06", "28 08", "22 Tara Street"]
        pos0 = ss[0].rfind(" ")  # reverse find starts at the end of string
        pos2 = ss[2].find(" ")
        pos1 = ss[1].find(" ")
        g = []
        g.append( ss[0][:pos0] )
        g.append( tomin( ss[0][pos0:]+":"+ss[1][:pos1] ))   # "06:28" --> whole minutes --> 388 min
        g.append( tomin( ss[1][pos1:]+":"+ss[2][:pos2] ))
        g.append( ss[2][pos2+1:] )
        allConn.append(g)
    return src, trg, allConn


# ---------------------------------------------------------------------------
#             M A I N
# ---------------------------------------------------------------------------

# load data
src, trg, allConn = getallConn()

print(allConn) # debug feature

# solve
bestConn1, bestConn2, bestTwhole, bestTdrive = sol(src, trg, allConn)
# debug printing:
#print(bestConn1)
#print(bestConn2)
#print(bestTwhole)
#print(bestTdrive)

# final print
printSol(bestConn1, bestConn2, bestTwhole, bestTdrive)




# ---------------------------------------------------------------------------
#       P R O B L E M    S T A T E M E N T
#              with  examples
# ---------------------------------------------------------------------------

'''

Hugo is going to travel by train from station A to station B.
Unfortunately, there is no direct train connection from A to B.
Hugo will have to go by train to another station C, and from there
take another train to B.
This means that the journey will consist of exactly two consecutive
train connections. It does not matter which station will be chosen
as a transfer station C.
Hugo has a list of all train connections between the stations
in the region.
He wants to complete the journey in a single day and he wants
the duration of the journey to be as short a possible.
However, he is limited by the transfer time.
He decided he will consider only those
pair of connections in which the second connection
departs at least 10 minutes after the arrival of the first connection.

The duration of a journey is the difference between the
arrival time of the second connection and the departure time of the
first connection.
The transfer duration of a journey is the difference between the
departure time of the second connection and the arrival time of the
first connection.

A journey X is considered better than a journey Y if and only if
either the duration of X is smaller than the duration of Y
or the duration of X and Y is the same and the transfer duration of
X is bigger than the transfer duration of Y.

Write a program that will automate the search for the
best connection.


Input
The first input line contains a positive integer N, the
number of connections in the list.
The second and the third line contain the name of the start
and the end station of the journey, respectively.
The list of connections occupies next N lines,
each line describes one connection.
A connection description consists of four items.
The first and the fourth items are different station names.
The second item is the departure time from the station specified in the first item.
The third item is the arrival time to the station specified in the fourth item.
Each time item consists of hour and minute specification, both are
two digits long and are separated by a colon.
All items on a line are separated by spaces.
The value of N is between 2 and 1000.
The station names do not contain digits or colons.

Output
The output consists of two lines.
The first line contains the duration of the best journey.
The the second line contains the duration of the shortest journey decreased
by the transfer duration of that journey.
Both durations are presented in the HH:MM format which is the same
as the format of times in the input.
It is guaranteed that there is always at least one best journey.




Example 1

Input
5
A
B
A 01:00 02:30 C   # in the program [A, 60, 150, c]
A 01:00 02:00 C
C 03:00 04:30 B
C 02:30 03:30 B
C 03:00 04:00 B

Output
02:30
02:00

Comment:
The connections in the best journey are
A 01:00 02:00 C
C 02:30 03:30 B


Example 2
Input
7
Mossley West
Ashtown
Mossley West 15:29 16:18 Titanic Quarter/Bridge End
Tara Street 08:33 09:54 Ashtown
Mossley West 06:28 08:22 Tara Street
Tara Street 12:40 14:34 Ashtown
Mossley West 09:55 12:27 Tara Street
Titanic Quarter/Bridge End 14:20 17:11 Tara Street
Mossley West 06:38 07:41 Tara Street

Output
03:16
02:24

Comment:
The connections in the best journey are
  Mossley West 06:38 07:41 Tara Street
  Tara Street 08:33 09:54 Ashtown


Example 3
Input

24
Mossley West
Newry
Longford 04:10 06:10 Newry
Newry 15:09 17:21 Fota
Fota 17:48 18:15 Mossley West
Newry 00:22 03:01 Glenageary
Longford 08:27 09:23 Newry
Mossley West 06:58 09:06 Fota
Newry 08:34 11:08 Glenageary
Longford 18:37 21:57 Glenageary
Longford 03:27 06:28 Fota
Longford 16:09 18:04 Newry
Glenageary 00:25 02:35 Mossley West
Glenageary 14:31 17:52 Mossley West
Mossley West 03:45 07:43 Glenageary
Mossley West 14:18 18:08 Fota
Glenageary 18:39 20:51 Newry
Fota 11:33 11:55 Longford
Glenageary 15:15 16:14 Newry
Newry 10:29 12:00 Fota
Mossley West 01:17 05:10 Longford
Newry 02:57 05:00 Longford
Glenageary 08:01 10:31 Mossley West
Mossley West 04:05 07:56 Glenageary
Glenageary 10:03 12:36 Mossley West


Output
08:06
04:49

Comment:
The connections in the best journey are
Mossley West 01:17 05:10 Longford
Longford 08:27 09:23 Newry




'''












