Buggy python submission (verdict: Runtime Error):
while True:
    n = input()
    if (n == 0):
        break
    d = []
    for i in range(n):
        a = map(int, raw_input().split())
        d.append(a[1:])
    for i in range(31):
        ok = True
        for j in d:
            t = min(j)
            if (i < t):
                ok = False
                break
        if (ok):
            print i
            break
    if (ok):
        continue
    else:
        print -1
    
        


            

Fixed submission:
while True:
    n = input()
    if (n == 0):
        break
    state = []
    schedule = []
    ok = True
    for i in range(n):
        temp = map(int, raw_input().split())
        if temp[0] != 0:
            schedule.append(set(temp[1:]))
        else:
            ok = False
        state.append(set([i]))
    if (ok == False):
        print -1
        continue
    for day in range(1, 31):
        temp = []
        Maps = set()
        for i in range(len(schedule)):
            if day in schedule[i]:
                temp.append(i)
                Maps |= state[i]
        for c in temp:
            state[c] = Maps
        if len(Maps) == n:
            print day
            break
    else:
        print -1

Buggy python submission (verdict: Runtime Error):
while True:
    n = input()
    if (n == 0):
        break
    d = []
    digit = set()
    for i in range(n):
        a = map(int, raw_input().split())
        for j in a[1:]:
            digit.add(j)
        d.append(a[1:])
    paper = [set() for i in range(n)]
    ans = 31
    for i in range(n):
        paper[i].add(i)

    for i in digit:
        for j in range(len(d)):
            if i in d[j]:
                for k in range(j, len(d)):
                    if i in d[k]:
                        for l in paper[k]:
                            paper[j].add(l)
                        for l in paper[j]:
                            paper[k].add(l)
        for j in range(len(d)):
            if (len(paper[j]) == n):
                ans = min(ans, i)

    if (ans != 31):
        print ans
    else:
        print -1

                
        

Fixed submission:
while True:
    n = input()
    if (n == 0):
        break
    state = []
    schedule = []
    ok = True
    for i in range(n):
        temp = map(int, raw_input().split())
        if temp[0] != 0:
            schedule.append(set(temp[1:]))
        else:
            ok = False
        state.append(set([i]))
    if (ok == False):
        print -1
        continue
    for day in range(1, 31):
        temp = []
        Maps = set()
        for i in range(len(schedule)):
            if day in schedule[i]:
                temp.append(i)
                Maps |= state[i]
        for c in temp:
            state[c] = Maps
        if len(Maps) == n:
            print day
            break
    else:
        print -1

Buggy python submission (verdict: Runtime Error):
while True:
    n = input()
    if (n == 0):
        break
    d = []
    digit = set()
    for i in range(n):
        a = map(int, raw_input().split())
        for j in a[1:]:
            digit.add(j)
        d.append(a[1:])
    paper = [set() for i in range(n)]
    ans = 31
    for i in range(n):
        paper[i].add(i)
    for i in digit:
        for j in range(len(d)):
            if i in d[j]:
                for k in range(j, len(d)):
                    if i in d[k]:
                        for l in paper[k]:
                            paper[j].add(l)
                        for l in paper[j]:
                            paper[k].add(l)
        for j in range(len(d)):
            if (len(paper[j]) == n):
                ans = min(ans, i)
    if (ans != 31):
        print ans
    else:
        print -1

                
        

Fixed submission:
while True:
    n = input()
    if (n == 0):
        break
    state = []
    schedule = []
    ok = True
    for i in range(n):
        temp = map(int, raw_input().split())
        if temp[0] != 0:
            schedule.append(set(temp[1:]))
        else:
            ok = False
        state.append(set([i]))
    if (ok == False):
        print -1
        continue
    for day in range(1, 31):
        temp = []
        Maps = set()
        for i in range(len(schedule)):
            if day in schedule[i]:
                temp.append(i)
                Maps |= state[i]
        for c in temp:
            state[c] = Maps
        if len(Maps) == n:
            print day
            break
    else:
        print -1