import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;
 
void main() {
    int N, P, Q;
    auto C = new bool[99 * 60 + 59];
    auto D = new bool[99 * 60 + 59];
    bool input() {
        string p, q;
        readf("%d %s %s\n", &N, &p, &q);
        if (N == 0 && p == "0" && q == "0") return false;
        P = p[0 .. 2].to!int * 60 + p[2 .. 4].to!int;
        Q = q[0 .. 2].to!int * 60 + q[2 .. 4].to!int;
        C[P .. Q] = false;
        foreach (i; 0 .. N) {
            int K; scanf("%d\n", &K);
            auto pq = readln.chomp.split(" ").array;
            int s, t;
            D[] = true;
            for (int j = 0; j < K * 2; j += 2) {
                s = pq[j][0 .. 2].to!int * 60 + pq[j][2 .. 4].to!int;
                t = pq[j + 1][0 .. 2].to!int * 60 + pq[j + 1][2 .. 4].to!int;
                D[s .. t] = false;
            }
            foreach (j; P .. Q) {
                if (D[j]) {
                    C[j] = true;
                }
            }
        }
        return true;
    }

    void solve() {
        writeln( reduce!((a, xs) => max(a, xs.length))(
                0L,
                C[P .. Q].map!((a) => cast(char)(a.to!int + '0')).split("0")));
    }
    
    while (input) solve;
}