Problem:


Boring Commercial

    Now it is spring holidays. A lazy student has finally passed all final examination, and he decided to just kick back and just watch TV all day. Oh, his
only source of entertainment is watching TV. And TV commercial, as usual, are
a big nuisance for him. He can watch any thing on television, but cannot bear
even a single second of commercial. So to prevent himself from the boredom of
seeing the boring commercial, he keeps shuffling through the TV channels, so
that he can watch programs on different channels without seeing even a single
commercial.

Given the number of channels, and the duration at which the TV commercials
are showed on each of the channels, you have to write a program which will
print the longest interval for which the lazy student can watch the television by
shuffling between the different channels without ever seeing an TV commercial.

    
For example, consider the simplified situation where there are only three
television channels, and suppose that he is watching TV from 2100 hrs to 2400
hrs. Suppose that the commercials are displayed at following time on each of
the channels.

 Channel 1: 2100 to 2130, 2200 to 2230 and 2300 to 2330

 Channel 2: 2130 to 2200, 2330 to 2400

 Channel 3: 2100 to 2130, 2330 to 2400

    Then in this case, he can watch TV without getting interrupted by commercials for full 3 hours by watching Channel 2 from 2100 to 2130, then Channel 3
from 2130 to 2330, and then Channel 1 from 2330 to 2400.

Input

The input will consist of several cases. In each case, the first line of the input
will be n, the number of channels, which will then be followed by p and q, the
time interval between which he will be watching the TV. It will be followed by
2n lines, giving the time slots for each of the channels. For each channel, the
first line will be k, the number of commercial slots, and it will then be followed
by 2k numbers giving the commercial slots in order.

    The input will be terminated by values 0 for each of n, p, q. This case should
not be processed.

Output

For each case, you have to output the maximum duration (in minutes) for which
he can watch television without seeing any commercial.

Sample Input

1 2100 2400
1
2130 2200
3 2100 2400
3
2100 2130 2200 2230 2300 2330
2
2130 2200 2330 2400
2
2100 2130 2330 2400
0 0 0

Output for the Sample Input

120
180



Sample input:
1 2100 2400
1
2130 2200
3 2100 2400
3
2100 2130 2200 2230 2300 2330
2
2130 2200 2330 2400
2
2100 2130 2330 2400
0 0 0


Sample output:
120
180

Submission status: Wrong Answer

Solution code:
#while 1:
    #n=map(int,raw_input().split())

def chang(x):
    h = x / 100
    m= x%100
    return h * 60 + m

while 1:
    (ch,st,en) = map(int, raw_input().split())
    if(ch==0 and st ==0 and en ==0):break
    hours = [0]*(chang(2400)+1)
    for i in range(ch):
        line = raw_input()
        tms = map(int, raw_input().split())
        for ti in tms[0::2]:
            hours[chang(ti)] += 1
            print ti,hours[chang(ti)]            
        for tim in tms[1::2]:
            hours[chang(tim)] -=1
            print tim,hours[chang(tim)]
    #end imos
    spn = 0
    checker = 0
    maxspn = 0
    for t in range(chang(st),chang(en)+1):
        if(checker < ch and hours[t] + checker >= ch):
            if(spn > maxspn):maxspn = spn
            spn=0
        checker += hours[t]
        if(checker < ch):spn+=1
    if(spn > maxspn):maxspn = spn
    
    print maxspn-1

Submission status: Wrong Answer

Solution code:
#while 1:
    #n=map(int,raw_input().split())

def chang(x):
    h = x / 100
    m= x%100
    return h * 60 + m

while 1:
    (ch,st,en) = map(int, raw_input().split())
    if(ch==0 and st ==0 and en ==0):break
    hours = [0]*(chang(2400)+1)
    for i in range(ch):
        line = raw_input()
        tms = map(int, raw_input().split())
        for ti in tms[0::2]:
            hours[chang(ti)] += 1
            #print ti,hours[chang(ti)]            
        for tim in tms[1::2]:
            hours[chang(tim)] -=1
            #print tim,hours[chang(tim)]
    #end imos
    spn = 0
    checker = 0
    maxspn = 0
    for t in range(chang(st),chang(en)+1):
        if(checker < ch and hours[t] + checker >= ch):
            if(spn > maxspn):maxspn = spn
            spn=0
        checker += hours[t]
        if(checker < ch):spn+=1
    if(spn > maxspn):maxspn = spn
    
    print maxspn-1

Submission status: Wrong Answer

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*60*24    
    for i in range(n):
        m = int(input())
        g = input().split()
        j = 0
        for a in g:
            a = int(a)
            if j%2:
                t[a//100*60+a%100] -= 1
            else:
                t[a//100*60+a%100] += 1
            j += 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if not cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(ma if ma > ans else ans)

Submission status: Wrong Answer

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*(60*24+1)
    for i in range(n):
        m = int(input())
        g = input().split()
        j = 0
        for a in g:
            a = int(a)
            if j%2:
                t[a//100*60+a%100] -= 1
            else:
                t[a//100*60+a%100] += 1
            j += 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if not cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(ma if ma > ans else ans)

Submission status: Wrong Answer

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*(60*24+1)
    for i in range(n):
        m = int(input())
        g = input().split()
        j = 0
        for a in g:
            a = int(a)
            if j%2:
                t[a//100*60+a%100] += 1
            else:
                t[a//100*60+a%100] -= 1
            j += 1
    cnt = n
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(ma if ma > ans else ans)

Submission status: Wrong Answer

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*(60*24+1)
    for i in range(n):
        m = int(input())
        g = input().split()
        j = 0
        for a in g:
            a = int(a)
            if j%2:
                t[a//100*60+a%100] += 1
            else:
                t[a//100*60+a%100] -= 1
            j += 1
    cnt = n
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(max(ma, ans))

Submission status: Wrong Answer

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int(e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*(60*24+1)
    for i in range(n):
        m = int(input())
        g = input().split()
        j = 0
        for o in g:
            a = int(o)
            if j%2:
                t[a//100*60+a%100] += 1
            else:
                t[a//100*60+a%100] -= 1
            j += 1
    cnt = n
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(ma if ma > ans else ans)

Submission status: Accepted

Solution code:
def cv(t):
	return t/100*60+t%100

while 1:
	n,p,q = map(int,raw_input().split())
	if n==0: break
	v=[0]*1440
	for i in range(n):
		k = int(raw_input())
		a = map(int,raw_input().split())
		for j in range(k):
			for l in range(cv(a[::2][j]),cv(a[1::2][j])):
				v[l]+=1
	m=c=0
	for i in range(cv(p),cv(q)):
		c=c+1 if v[i]<n else 0
		m=max(m,c)
	print m

Submission status: Accepted

Solution code:
def get():
	return map(int,raw_input().split())
def cv(t):
	return t/100*60+t%100
while 1:
	n,p,q=get()
	if n==0: break
	v=[0]*1440
	for i in range(n):
		k=get()[0]; a=get()
		for j in range(k):
			for l in range(cv(a[::2][j]),cv(a[1::2][j])):
				v[l]+=1
	m=c=0
	for i in range(cv(p),cv(q)):
		c=(c+1)*(v[i]<n)
		m=max(m,c)
	print m

Submission status: Accepted

Solution code:
def tt(o,t):
    om=o%100
    oh=o/100
    tm=t%100
    th=t/100
    return tm-om+(th-oh)*60
    

while(1):
    [c,s,e]=[int(x) for x in raw_input().split()]
    if c==0:
        break
    else:
        cmlist=[]
        timelist=[s]
        state=2**c-1
        oldtime=s
        oldstate=0
        ans=0
        temptv=0
        for i in range(c):
            numofcom=int(raw_input())
            aa=[int(x) for x in raw_input().split()]
            cmlist.append(aa)
            timelist=timelist+aa
        timelist.append(e)
        timelist=sorted(list(set(timelist)))
        for time in timelist:
            for i in range(c):
                if len(cmlist[i])>0:
                    if cmlist[i][0]==time:
                        state=state^(2**i)
                        cmlist[i].pop(0)
            if oldstate!=0:
                temptv=temptv+tt(oldtime,time)
            else:
                ans=max(ans,temptv)
                temptv=0
            oldtime=time
            oldstate=state
            #print time,bin(state),temptv
        ans=max(ans,temptv)
        print ans

Submission status: Accepted

Solution code:
#while 1:
    #n=map(int,raw_input().split())

def chang(x):
    h = x / 100
    m= x%100
    return h * 60 + m

while 1:
    (ch,st,en) = map(int, raw_input().split())
    if(ch==0 and st ==0 and en ==0):break
    hours = [0]*(chang(2400)+1)
    for i in range(ch):
        line = raw_input()
        tms = map(int, raw_input().split())
        for ti in tms[0::2]:
            hours[chang(ti)] += 1
            #print ti,hours[chang(ti)]            
        for tim in tms[1::2]:
            hours[chang(tim)] -=1
            #print tim,hours[chang(tim)]
    #end imos
    spn = 0
    checker = 0
    maxspn = 0
    for t in range(chang(st),chang(en)):
        if(checker < ch and hours[t] + checker >= ch):
            if(spn > maxspn):maxspn = spn
            spn=0
        checker += hours[t]
        if(checker < ch):spn+=1
    if(spn > maxspn):maxspn = spn
    
    print maxspn

Submission status: Accepted

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int(e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*(60*24+1)
    for i in range(n):
        m = int(input())
        g = input().split()
        j = 0
        for o in g:
            a = int(o)
            if j%2:
                t[a//100*60+a%100] += 1
            else:
                t[a//100*60+a%100] -= 1
            j += 1
    cnt = n
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
            ans = 0
    print(ma if ma > ans else ans)

Submission status: Accepted

Solution code:
#-*- coding:utf-8 -*-

#入力数値を分単位に変更
def convert_time(n):
    return (n/100)*60 + n%100

#コマーシャルの共通部分を返す
def kyotsu(cm1,cm2):
    result = []
    for i in range(len(cm2)/2):
        #cm2の各CMとかぶっている部分を取得
        for j in range(len(cm1)/2):
            stt = max(cm2[i*2],cm1[j*2])
            edt = min(cm2[i*2+1],cm1[j*2+1])
            if edt - stt > 0:
                result.append(stt)
                result.append(edt)

    return result

#コマーシャルを見ずにテレビを見られる最長時間を求める
while 1:
    cond = [int(r) for r in raw_input().split()]
    if cond[0]==cond[1]==cond[2]==0: #入力がすべて0なら終了
        break

    #開始時間、終了時間を算出
    start = convert_time(cond[1])
    end = convert_time(cond[2])

    cms = []
    #各チャンネルのコマーシャル時間を求める
    for i in range(cond[0]):
        n = int(raw_input()) #コマーシャル数
        input = [convert_time(int(r)) for r in raw_input().split()]
        j=0
        cmstmp = []
        while j < n:
            st = input[2*j]
            ed = input[2*j+1]
            if not i:
                cms.append(st)
                cms.append(ed)
            else:
                cmstmp.append(st)
                cmstmp.append(ed)
            j += 1
        if i > 0:
            cms = kyotsu(cms,cmstmp)


    #cmsを利用してコマーシャルを見なくてすむ最大時間を出力
    cms = [start] + cms + [end]
    print max([cms[i*2+1]-cms[i*2] for i in range(len(cms)/2)])

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

while True:
    n, p, q = map(int, raw_input().split())
    if n == p == q == 0:
        break
    T = [0 for i in range(2401)]
    for loop in range(n):
        k = input()
        ts = map(int, raw_input().split())
        for i in range(k):
            T[ts[i * 2]] += 1
            T[ts[i * 2 + 1]] -= 1
    cnt = 0
    S = 0
    ans = 0
    for i in range(24):
        for j in range(60):
            t = i * 100 + j
            if p <= t < q:
                cnt += T[t]
                if cnt < n:
                    S += 1
                else:
                    ans = max(ans, S)
                    S = 0
    ans = max(ans, S)
    print ans

Submission status: Accepted

Solution code:
conv = lambda t: (t // 100)*60 + (t % 100)
while 1:
    N, P, Q = map(int, input().split())
    if N == P == Q == 0:
        break
    P = conv(P); Q = conv(Q)
    CM = [0]*(Q-P+1)
    for i in range(N):
        k = int(input())
        *T, = map(int, input().split())
        for s, t in zip(T[::2], T[1::2]):
            s = conv(s); t = conv(t)
            CM[s-P] += 1
            CM[t-P] -= 1
    cnt = ans = 0
    for i in range(Q-P):
        CM[i+1] += CM[i]
        if CM[i] < N:
            cnt += 1
        else:
            cnt = 0
        ans = max(ans, cnt)
    print(ans)

Submission status: Accepted

Solution code:
# AOJ 1006: Boring Commercials
# Python3 2018.7.5 bal4u

import sys
from sys import stdin
input = stdin.readline

def d2t(d):	return (d//100)*60 + (d%100)

while True:
	n, p, q = map(int, input().split())
	if n == 0: break
	tbl = [0]*1500
	p, q = d2t(p), d2t(q)
	for i in range(n):
		k = 2*int(input())
		a = list(map(int, input().split()))
		for j in range(0, k, 2):
			t1, t2 = d2t(a[j]), d2t(a[j+1])
			for t in range(t1, t2): tbl[t] += 1
	ans = k = 0
	for t in range(p, q):
		if tbl[t] < n: k += 1
		elif k > 0: 
			if k > ans: ans = k
			k = 0
	if k > ans: ans = k
	print(ans)

Submission status: Runtime Error

Solution code:
def cv(t):
	return t/100*60+t%100

while 1:
	n=input(); p=input(); q=input()
	if n==0: break
	v=[0]*1440
	for i in range(n):
		k=input()
		for j in range(k):
			a=input(); b=input();
			for l in range(cv(a),cv(b)):
				v[l]+=1
	m=c=0
	for i in range(cv(p),cv(q)):
		c=c+1 if v[i]<n else 0
		m=max(m,c)
	print m

Submission status: Runtime Error

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*60*24    
    for i in range(n):
        m = int(input())
        for j in range(m):
            ss,ee = input().split()
            ss = int(ss)
            ee = int(ee)
            t[ss//100*60+ss%100] += 1
            t[ee//100*60+ee%100] -= 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if not cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(ma if ma > ans else ans)

Submission status: Runtime Error

Solution code:
while True:
    a,b,c = input().split()
    n = int(a)
    s = int(b)
    e = int (c)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*60*24    
    for i in range(n):
        m = int(input())
        for j in range(m):
            a,b = input().split()
            ss = int(a)
            ee = int(b)
            t[ss//100*60+ss%100] += 1
            t[ee//100*60+ee%100] -= 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if not cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(ma if ma > ans else ans)

Submission status: Runtime Error

Solution code:
while True:
    a,b,c = input().split()
    n = int(a)
    s = int(b)
    e = int (c)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*60*24    
    for i in range(n):
        m = int(input())
        for j in range(m):
            a,b = input().split()
            ss = int(a)
            ee = int(b)
            t[ss//100*60+ss%100] += 1
            t[ee//100*60+ee%100] -= 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if cnt==0:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
    print(ma if ma > ans else ans)

Submission status: Runtime Error

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*60*24    
    for i in range(n):
        m = int(input())
        for j in range(m):
            ss,ee = input().split()
            ss = int(ss)
            ee = int(ee)
            t[ss//100*60+ss%100] += 1
            t[ee//100*60+ee%100] -= 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if not cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
        if ma < ans:
            ma = ans
    print(ma)

Submission status: Runtime Error

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = s//100*60+s%100
    e = e//100*60+e%100
    t = [0]*60*24    
    for i in range(n):
        m = int(input())
        for j in range(m):
            ss,ee = input().split()
            ss = int(ss)
            ee = int(ee)
            t[ss//100*60+ss%100] += 1
            t[ee//100*60+ee%100] -= 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if not cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
        if ma < ans:
            ma = ans
    print(ma)

Submission status: Runtime Error

Solution code:
while True:
    n,s,e = input().split()
    n = int(n)
    s = int(s)
    e = int (e)
    if not n+s+e:
        break
    s = (s//100)*60+s%100
    e = (e//100)*60+e%100
    t = [0]*60*24    
    for i in range(n):
        m = int(input())
        for j in range(m):
            ss,ee = input().split()
            ss = int(ss)
            ee = int(ee)
            t[(ss//100)*60+ss%100] += 1
            t[(ee//100)*60+ee%100] -= 1
    cnt = 0
    ans = 0
    ma = -1
    for i in range(e):
        cnt += t[i]
        if i < s:
            continue
        if not cnt:
            ans += 1
        else:
            if ma < ans:
                ma = ans
                ans = 0
        if ma < ans:
            ma = ans
    print(ma)

Submission status: Runtime Error

Solution code:
from queue import deque


def to_tv_times(cm_times):
    if cm_times[0] <= p:
        cm_times.popleft()
    else:
        cm_times.appendleft(p)
    if cm_times[-1] >= q:
        cm_times.pop()
    else:
        cm_times.append(q)
    return cm_times


def to_minute(hhmm):
    return hhmm // 100 * 60 + hhmm % 100


while True:
    n, p, q = map(int, input().split())
    if n == 0:
        break
    cm_times = deque([p, q])
    for _ in range(n):
        if not cm_times:
            input()
            input()
            continue
        k = int(input())
        tv_times = to_tv_times(deque(map(int, input().split())))
        for tvs, tvt in zip(*[iter(tv_times)] * 2):
            new_cm_times = deque()
            while cm_times:
                cms = cm_times.popleft()
                if tvt < cms:
                    cm_times.appendleft(cms)
                    break
                cmt = cm_times.popleft()
                if cmt < tvs:
                    new_cm_times.append(cms)
                    new_cm_times.append(cmt)
                    continue
                if cms < tvs:
                    new_cm_times.append(cms)
                    new_cm_times.append(tvs)
                    if tvt < cmt:
                        cm_times.appendleft(cmt)
                        cm_times.appendleft(tvt)
                        break
                    elif tvt == cmt:
                        break
                    else:
                        continue
                else:
                    if cmt < tvt:
                        continue
                    elif cmt == tvt:
                        break
                    else:
                        cm_times.appendleft(cmt)
                        cm_times.appendleft(tvt)
                        break
            cm_times = new_cm_times + cm_times
    if not cm_times:
        print(to_minute(q) - to_minute(p))
    else:
        final_tv_times = to_tv_times(cm_times)
        print(max(t - s for s, t in zip(*[map(to_minute, final_tv_times)] * 2)))

Submission status: Runtime Error

Solution code:
#-*- coding:utf-8 -*-

#入力数値を分単位に変更
def convert_time(n):
    return (n/100)*60 + n%100

#コマーシャルの共通部分を返す
def kyotsu(cm1,cm2):
    result = []
    for i in range(len(cm2)/2):
        #cm2の各CMとかぶっている部分を取得
        for j in range(len(cm1)/2):
            st = max(cm2[i*2],cm1[j*2])
            ed = min(cm2[i*2+1],cm1[j*2+1])
            if ed - st > 0:
                result.append(st)
                result.append(ed)

    return result

#コマーシャルを見ずにテレビを見られる最長時間を求める
while 1:
    cond = [int(r) for r in raw_input().split(" ")]
    if cond[0]==cond[1]==cond[2]==0: #入力がすべて0なら終了
        break

    #開始時間、終了時間を算出
    start = convert_time(cond[1])
    end = convert_time(cond[2])

    cms = []
    #各チャンネルのコマーシャル時間を求める
    for i in range(cond[0]):
        n = int(raw_input()) #コマーシャル数
        input = [convert_time(int(r)) for r in raw_input().split(" ")]
        j=0
        cmstmp = []
        while j < n:
            st = input[2*j]
            ed = input[2*j+1]
            if i==0:
                cms.append(st)
                cms.append(ed)
            else:
                cmstmp.append(st)
                cmstmp.append(ed)
            j += 1
        if i>0:
            cms = kyotsu(cms,cmstmp)


    #cmsを利用してコマーシャルを見なくてすむ最大時間を出力
    cms = [start] + cms + [end]
    print max([cms[i*2+1]-cms[i*2] for i in range(len(cms)/2)])

Submission status: Runtime Error

Solution code:
#-*- coding:utf-8 -*-

#入力数値を分単位に変更
def convert_time(n):
    return (n/100)*60 + n%100

#コマーシャルの共通部分を返す
def kyotsu(cm1,cm2):
    result = []
    for i in range(len(cm2)/2):
        #cm2の各CMとかぶっている部分を取得
        for j in range(len(cm1)/2):
            st = max(cm2[i*2],cm1[j*2])
            ed = min(cm2[i*2+1],cm1[j*2+1])
            if ed - st > 0:
                result.append(st)
                result.append(ed)

    return result

#コマーシャルを見ずにテレビを見られる最長時間を求める
while 1:
    cond = [int(r) for r in raw_input().split(" ")]
    if cond[0]==cond[1]==cond[2]==0: #入力がすべて0なら終了
        break

    #開始時間、終了時間を算出
    start = convert_time(cond[1])
    end = convert_time(cond[2])

    cms = []
    #各チャンネルのコマーシャル時間を求める
    for i in range(cond[0]):
        n = int(raw_input()) #コマーシャル数
        input = [convert_time(int(r)) for r in raw_input().split(" ")]
        j=0
        cmstmp = []
        while j < n:
            st = input[2*j]
            ed = input[2*j+1]
            if not i:
                cms.append(st)
                cms.append(ed)
            else:
                cmstmp.append(st)
                cmstmp.append(ed)
            j += 1
        if i > 0:
            cms = kyotsu(cms,cmstmp)


    #cmsを利用してコマーシャルを見なくてすむ最大時間を出力
    cms = [start] + cms + [end]
    print max([cms[i*2+1]-cms[i*2] for i in range(len(cms)/2)])