Absent-minded Masha got set of n cubes for her birthday.

At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x.

To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number.

The number can't contain leading zeros. It's not required to use all cubes to build a number.

Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations.


-----Input-----

In first line integer n is given (1 ≤ n ≤ 3) — the number of cubes, Masha got for her birthday.

Each of next n lines contains 6 integers a_{i}_{j} (0 ≤ a_{i}_{j} ≤ 9) — number on j-th face of i-th cube.


-----Output-----

Print single integer — maximum number x such Masha can make any integers from 1 to x using her cubes or 0 if Masha can't make even 1.


-----Examples-----
Input
3
0 1 2 3 4 5
6 7 8 9 0 1
2 3 4 5 6 7

Output
87
Input
3
0 1 3 5 6 8
1 2 4 5 7 8
2 3 4 6 7 9

Output
98


-----Note-----

In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
a = sorted([list(map(int, input().split())) for i in range(n)])

import itertools
for x in range(1,10**n):
  good = False
  s = str(x)
  for p in itertools.permutations(a, len(s)):
    good |= all([int(s[i]) in v for i, v in enumerate(p)])
  if not good:
    print(x-1)
    return
    
print((10**n)-1)

def gen(cur, used, x):
    pos.add(cur)
    if x == n:
        return
    for j in range(n):
        if not used[j]:
            for i in a[j]:
                if i != 0 or x != 0:
                    used[j] = True
                    gen(cur * 10 + i, used, x + 1)
                    used[j] = False


n = int(input())
a = []
for i in range(n):
    a.append(list(map(int, input().split())))
pos = set()
gen(0, [False] * n, 0)
x = 1
while x in pos:
    x += 1
print(x - 1)


n = int(input())
cs = []
for i in range(n):
    cs.append([int(x) for x in input().split()])

ans = 0


def can_make(num, cs):
    if num == 0:
        return True
    elif len(cs) == 0:
        return False
    cm = False
    for i in range(len(cs)):
        if num%10 in cs[i]:
            if can_make(num//10, cs[0:i]+cs[i+1:]):
                return True
    return False
        
    
    
for i in range(1,1000):
    if can_make(i, cs):
        ans = i
    else:
        break

print(ans)


def f(m):
    m = list(map(int, list(str(m))))
    #print(m)
    for k in range(dd[n]):
        #print(d[k])
        cur = 0
        for j in range(n):
            kub = d[k][j] - 1
            if cur == len(m):
                return True
            if m[cur] in a[kub]:
                cur += 1
        if cur == len(m):
            return True
    return False
                

n = int(input())
a = [[], [], []]
for i in range(n):
    a[i] = list(map(int, input().split()))
d = [[1, 2, 3], [2, 1, 3], [1, 3, 2], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
dd = [0, 0, 2, 6]
if n == 1:
    for j in range(1, 10):
        if j not in a[0]:
            print(j - 1)
            return
ans = 0
while f(ans + 1):
    ans += 1
#print(f(88))
print(ans)



# -*- coding: utf-8 -*-

import math
import collections
import bisect
import heapq
import time
import random
import itertools
import sys

"""
created by shhuan at 2017/11/4 00:13

"""

N = int(input())

A = []
for i in range(N):
    A.append([int(x) for x in input().split()])


def dfs(A, index, p):
    if index >= len(A):
        v = 0
        for u in p:
            v *= 10
            v += u
        return {v}

    ans = set()
    for v in A[index]:
        ans |= dfs(A, index+1, p+[v])
    ans |= dfs(A, index+1, p)

    return ans

allNums = set()
for a in itertools.permutations(A, len(A)):
    allNums |= dfs(a, 0, [])

ans = 0
for i in range(1, max(allNums)+1):
    if i not in allNums:
        break
    ans = i

print(ans)







q=int(input())
a=[False for i in range(0,1002)]
s=[]
for i in range(0,q):
    s.append(list(map(int,input().split())))
for i in s:
    for j in i:
        a[j]=True
if q==2:
    for i in s[0]:
        for j in s[1]:
            a[i*10+j],a[j*10+i]=True,True
if q==3:
    for i in s[0]:
        for j in s[1]:
            a[i*10+j],a[j*10+i]=True,True
    for i in s[0]:
        for j in s[2]:
            a[i*10+j],a[j*10+i]=True,True
    for i in s[1]:
        for j in s[2]:
            a[i*10+j],a[j*10+i]=True,True
    for i in s[0]:
        for j in s[1]:
            for k in s[2]:
                a[k*100+i*10+j],a[k*100+j*10+i]=True,True
                a[k*10+i*100+j],a[k+j*10+i*100]=True,True
                a[k+i*10+j*100],a[k*10+i+j*100]=True,True
z=0
while (a[z+1]):
    z+=1
print(z)


n = int(input())
data = [[0] * 6 for i in range(n)]
for i in range(n):
    data[i] = list(map(int, input().split()))
mb = set()
for i in range(n):
    for j in range(6):
        mb.add(data[i][j])
for i in range(n):
    for j in range(n):
        for k in range(6):
            for k2 in range(6):
                if(i != j):
                    mb.add(data[i][k] * 10 + data[j][k2])
                    mb.add(data[i][k] + data[j][k2] * 10)
if (n == 3):
    for k1 in range(6):
        for k2 in range(6):
            for k3 in range(6):
                mb.add(data[0][k] * 100 + data[1][k2] * 10 + data[2][k3])
                mb.add(data[0][k] * 100 + data[1][k2] + data[2][k3] * 10)
                mb.add(data[0][k] * 10 + data[1][k2] * 100 + data[2][k3])
                mb.add(data[0][k] + data[1][k2] * 100 + data[2][k3] * 10)
                mb.add(data[0][k] * 10 + data[1][k2] + data[2][k3] * 100)
                mb.add(data[0][k] + data[1][k2] * 10 + data[2][k3] * 100)
i = 1
while i in mb:
    i += 1
print(i - 1)

n = int(input())
a = []
alll = []
for i in range(n):
    a += [list(map(str, input().split()))]
a += [[]]
a += [[]]
t = 1
while t != 1000:
    dt = str(t)
    if len(dt) == 1 and dt in a[0] + a[1] + a[2]:
        pass
    elif len(dt) == 1:
        break
    if len(dt) == 2:
        tt = False
        for i in range(3):
            j, k = 0, 0
            while j == i:
                j += 1
            while k == i or k == j:
                k += 1
            if dt[0] in a[i] and (dt[1] in a[j] or dt[1] in a[k]):
                tt = True
        if not tt:
            break
    if len(dt) == 3:
        tt = False
        for i in range(3):
            for j in range(i + 1, 3):
                k = 0
                while k == i or k == j:
                    k += 1
                if dt[0] in a[i] and dt[1] in a[j] and dt[2] in a[k]:
                    tt = True
        if not tt:
            break

    t += 1
print(t - 1)


import itertools

kostki = []

for i in range(int(input())):
    kostki.append(input().split())

l = ([ ''.join(i) for i in itertools.product(*kostki) ])

s = [
    (1,2,3), (1,2),(1,3),(2,3),(1,),(2,),(3,)
]
s1 = []
for p in s:
    s1 += itertools.permutations(p)

res = set()
for inp in l:
    for p in s1:
        num = ''
        for ind in p:
            if ind <= len(inp):
                num += inp[ind-1]
        if num:
            res.add(int(num))

#print(res)
for i in range(1, 1003):
    if i not in res:
        print(i-1)
        break


import itertools

n=int(input())
cubes=[]
for i in range(n):
    line=input().split()
    cubes.append(line)

nums={}
for i in range(1,1000):
    nums[i]=False

if n==3:
    for a in cubes[0]:
        for b in cubes[1]:
            for c in cubes[2]:
                for i in range(n):
                    for comb in itertools.permutations([a,b,c],i+1):
                        pres=0
                        for num in comb:
                            pres*=10
                            pres+=int(num)
                        nums[pres]=True


if n==2:
    for a in cubes[0]:
        for b in cubes[1]:
            for i in range(n):
                for comb in itertools.permutations([a,b],i+1):
                    pres=0
                    for num in comb:
                        pres*=10
                        pres+=int(num)
                    nums[pres]=True

if n==1:
    for a in cubes[0]:
        nums[int(a)]=True

x=0
while(nums[x+1]):
    x+=1
print(x)


from itertools import permutations

n = int(input())

l = [[int(x) for x in input().split()] for _ in range(n)]

s = set()

def add(t, cur, lvl):
	for i in range(6):
		print
		cur2 = cur * 10 + l[t[lvl]][i]
		s.add(cur2)
		if lvl < n - 1:
			add(t, cur2, lvl + 1)

for t in permutations(tuple(range(n))):
	add(t, 0, 0)

a = 1
while True:
	if a in s:
		a += 1
	else:
		print(a - 1)
		break

from itertools import permutations as perms
read = lambda: tuple(map(int, input().split()))
n = read()[0]
cbs = []
for i in range(n):
    cbs += [read()]
    
for i in range(1, 10000):
    ds = str(i)
    ch = False
    for p in perms(cbs):
        ii = []
        for k in range(len(ds)):
            for ci in range(n):
                if int(ds[k]) in p[ci] and not ci in ii:
                    ii += [ci]
                    break
        if len(ii) == len(ds):
            ch = True
            break
    if not ch:
        print(i - 1)
        break
    


from itertools import permutations

n = int(input())
a = []
for i in range(n):
    a.append(list(input().split()))
pa = list(permutations(a))

for x in range(1, 1000):
    ds = str(x)
    for a in pa:
        for d, c in zip(ds, a):
            if d not in c:
                break
        else:
            break
    else:
        break

print(x-1)


cubecount = input()
cubies = []
for i in range(int(cubecount)):
    cubies.append(input().split(" "))

count = 0
while count < 1000:
    p = False
    candidate = str(count+1)
    if count+1 < 10:
        for cube in cubies:
            if candidate in cube:
                p = True
    elif count+1 < 100:
        for cube in cubies:
            if candidate[0] in cube:
                cubieb = cubies.copy()
                cubieb.remove(cube)
                for cubeb in cubieb:
                    if candidate[1] in cubeb:
                        p = True
    else:
        for cube in cubies:
            if candidate[0] in cube:
                cubieb = cubies.copy()
                cubieb.remove(cube)
                for cubeb in cubieb:
                    if candidate[1] in cubeb:
                        cubiec = cubieb.copy()
                        cubiec.remove(cubeb)
                        for cubec in cubiec:
                            if candidate[2] in cubec:
                                p = True          
    if p:
        count +=1
    else:
        break

print(count)


table=[str(i) for i in range(1, 1000)]
n=int(input())
num=[]
res=[]
for i in range(n):
	s=input()
	s=s.split()
	num.append(s)
#print(num)
for i in num[0]:
	res.append(i)
if n>1:
	for i in num[0]:
		for j in num[1]:
			res.append(str(int(i+j)))
			res.append(str(int(j+i)))
	for i in num[1]:
		res.append(str(int(i)))
if n==3:
	for i in num[0]:
		for j in num[1]:
			for k in num[2]:
				res.append(str(int(i+j+k)))
				res.append(str(int(i+k+j)))
				res.append(str(int(j+i+k)))
				res.append(str(int(j+k+i)))
				res.append(str(int(k+i+j)))
				res.append(str(int(k+j+i)))
	for i in num[2]:
		res.append(str(int(i)))
	for i in num[0]:
		for j in num[2]:
			res.append(str(int(i+j)))
			res.append(str(int(j+i)))
	for i in num[1]:
		for j in num[2]:
			res.append(str(int(i+j)))
			res.append(str(int(j+i)))
#print(res)
for i in range(1, 1001):
	if not (str(i) in res):
		print(i-1)
		break


n = int(input().strip())
sets = [set([i for i in input().strip().split()]) for j in range(n)]

def get_all_perm(kek):
    if (len(kek) == 1):
        return [kek]
    ans = []
    for i in range(len(kek)):
        p = [i for i in kek]
        p.pop(i)
        for perm in get_all_perm(p):
            ans.append([kek[i]] + perm)
    return ans

answer = 0
while answer < 10**n:
    answer += 1
    g = list(str(answer))
    for perm in get_all_perm(sets):
        #print(perm)
        #print([perm[i] in sets[i] for i in range(len(perm))])
        if all([g[i] in perm[i] for i in range(len(g))]):
            break
    else:
        break

print(answer - 1)

n = int(input())
aij = [list(map(str,input().split())) for i in range(n)]
numbers = [0]*1001
numbers[0] = 1
for i in range(n):
    for j in range(6):
        numbers[int(aij[i][j])] = 1
if n == 2:
    for i in range(6):
        for j in range(6):
            numbers[int(aij[0][i]+aij[1][j])] = 1
            numbers[int(aij[1][j]+aij[0][i])] = 1
if n == 3:
        for i in range(6):
            for j in range(6):
                numbers[int(aij[0][i]+aij[1][j])] = 1
                numbers[int(aij[1][j]+aij[0][i])] = 1
        for i in range(6):
            for j in range(6):
                numbers[int(aij[1][i]+aij[2][j])] = 1
                numbers[int(aij[2][j]+aij[1][i])] = 1
        for i in range(6):
            for j in range(6):
                numbers[int(aij[0][i]+aij[2][j])] = 1
                numbers[int(aij[2][j]+aij[0][i])] = 1
        for i in range(6):
            for j in range(6):
                for z in range(6):
                    numbers[int(aij[0][i]+aij[1][j]+aij[2][z])] = 1
                    numbers[int(aij[0][i]+aij[2][z]+aij[1][j])] = 1
                    numbers[int(aij[1][j]+aij[0][i]+aij[2][z])] = 1
                    numbers[int(aij[1][j]+aij[2][z]+aij[0][i])] = 1
                    numbers[int(aij[2][z]+aij[1][j]+aij[0][i])] = 1
                    numbers[int(aij[2][z]+aij[0][i]+aij[1][j])] = 1
ans = -1
for i in range(1001):
    if numbers[i] == 0:
        break
    ans += 1
print(ans)


n = int(input())
a = []
a += [list(input())]
if (n > 1):
    a += [list(input())]
if (n > 2):
    a += [list(input())]
i = 1
while (i < 1000):
    s = list(str(i))
    if (n == 1):
        if (s[0] in a[0]):
            i += 1
            continue
    if (n == 2):
        if (len(s) == 1 and (s[0] in a[0] or s[0] in a[1])):
            i += 1
            continue
        if (len(s) == 2 and (
            (s[0] in a[0] and s[1] in a[1]) or
            (s[0] in a[1] and s[1] in a[0]))):
            i += 1
            continue
    if (n == 3):
        if (len(s) == 1 and (s[0] in a[0] or s[0] in a[1] or s[0] in a[2])):
            i += 1
            continue
        if (len(s) == 2 and (
            (s[0] in a[0] and s[1] in a[1]) or
            (s[0] in a[0] and s[1] in a[2]) or
            (s[0] in a[1] and s[1] in a[0]) or
            (s[0] in a[1] and s[1] in a[2]) or
            (s[0] in a[2] and s[1] in a[0]) or
            (s[0] in a[2] and s[1] in a[1]))):
            i += 1
            continue
        if (len(s) == 3 and (
            (s[0] in a[0] and s[1] in a[1] and s[2] in a[2]) or
            (s[0] in a[0] and s[1] in a[2] and s[2] in a[1]) or
            (s[0] in a[1] and s[1] in a[0] and s[2] in a[2]) or
            (s[0] in a[1] and s[1] in a[2] and s[2] in a[0]) or
            (s[0] in a[2] and s[1] in a[0] and s[2] in a[1]) or
            (s[0] in a[2] and s[1] in a[1] and s[2] in a[0])
            )):
            i += 1
            continue
    print(i-1)
    break


n = int(input())

cubes = []
for i in range(n):
	cubes.append(list(map(int, input().split())))

numbers = set()

if n == 3:
	for i in range(6):
		numbers.add(cubes[0][i])
		numbers.add(cubes[1][i])
		numbers.add(cubes[2][i])
		for j in range(6):
			numbers.add(cubes[0][i] * 10 + cubes[1][j])
			numbers.add(cubes[0][i] * 10 + cubes[2][j])
			numbers.add(cubes[1][i] * 10 + cubes[0][j])
			numbers.add(cubes[1][i] * 10 + cubes[2][j])
			numbers.add(cubes[2][i] * 10 + cubes[0][j])
			numbers.add(cubes[2][i] * 10 + cubes[1][j])
			for k in range(6):
				numbers.add(cubes[0][i] * 100 + cubes[1][j] * 10 + cubes[2][k])
				numbers.add(cubes[0][i] * 100 + cubes[2][j] * 10 + cubes[1][k])
				numbers.add(cubes[1][i] * 100 + cubes[0][j] * 10 + cubes[2][k])
				numbers.add(cubes[1][i] * 100 + cubes[2][j] * 10 + cubes[0][k])
				numbers.add(cubes[2][i] * 100 + cubes[0][j] * 10 + cubes[1][k])
				numbers.add(cubes[2][i] * 100 + cubes[1][j] * 10 + cubes[0][k])
if n == 2:
	for i in range(6):
		numbers.add(cubes[0][i])
		numbers.add(cubes[1][i])
		for j in range(6):
			numbers.add(cubes[0][i] * 10 + cubes[1][j])
			numbers.add(cubes[1][i] * 10 + cubes[0][j])
if n == 1:
	for i in range(6):
		numbers.add(cubes[0][i])

ans = 0
for i in range(1, 999):
	if i not in numbers:
		break
	ans = i

print(ans)

n=int(input())
def find(x,d=[]):
    if x==0 and d!=[]:
        return True
    l=x%10
    
    i=-1
    a=False
    for lis in s:
        i+=1
        if l in lis and i not in d:
            a=max(a,find(x//10,d+[i]))
    return a
s=[list(map(int,input().split())) for x in range(n)]
e=1
while find(e):
    e+=1
print(int(e-1  if find(1) else 0))

import sys
n = int(input())
max = 0
if n == 1:
    a = [int(x) for x in input().split()]
    for i in range(1, 10):
        if i not in a:
            print(max)
            return
        max += 1
elif n == 2:
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    for i in range(1, 10):
        if (i not in a) and (i not in b):
            print(max)
            return
        max += 1
    for i in range(10, 100):
        a1 = int(str(i)[0])
        a2 = int(str(i)[1])
        if ((a1 in a) and (a2 in b)) or ((a1 in b) and (a2 in a)):
            max += 1
        else:
            print(max)
            return
elif n == 3:
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]
    c = [int(x) for x in input().split()]
    for i in range(1, 10):
        if (i not in a) and (i not in b) and (i not in c):
            print(max)
            return
        max += 1
    for i in range(10, 100):
        a1 = int(str(i)[0])
        a2 = int(str(i)[1])
        if ((a1 in a) and (a2 in b)) or ((a1 in b) and (a2 in a)) or ((a1 in a) and (a2 in c)) or ((a1 in c) and (a2 in a)) or (a1 in b) and (a2 in c) or (a1 in c) and (a2 in b):
            max += 1
        else:
            print(max)
            return

c=[0 for i in range(1000)]
n=int(input())
cc=[]
for i in range(n):
    a=list(map(int,input().strip().split(' ')))
    for j in a:
        c[j]=1
    cc.append(a)
if(n==1):
    for i in range(1,1000):
        if(c[i]==0):
            break
    print(i-1)
if(n==2):
    a=cc[0]
    b=cc[1]
    for i in a:
        for j in b:
            c[i*10+j]=1
            c[j*10+i]=1
    for i in range(1,1000):
        if(c[i]==0):
            break
    print(i-1)
if(n==3):
    a=cc[0]
    b=cc[1]
    d=cc[2]
    
    for i in a:
        for j in b:
            c[i*10+j]=1
            c[j*10+i]=1
    for i in a:
        for j in d:
            c[i*10+j]=1
            c[j*10+i]=1
    for i in d:
        for j in b:
            c[i*10+j]=1
            c[j*10+i]=1
    for i in a:
        for j in b:
            for k in d:
                c[i*100+j*10+k]=1
                c[i*100+k*10+j]=1
                c[j*100+i*10+k]=1
                c[j*100+i+k*10]=1
                c[k*100+i*10+j]=1
                c[k*100+j*10+i]=1
    
    for i in range(1,1000):
        if(c[i]==0):
            break
    print(i-1)



def fun(n,l):
    if len(l)==1:
        return n in l[0]
    elif len(l)==2:
        if n<10:
            return (n in l[0]) or (n in l[1])
        elif n<100:
            u=n%10
            h=int((str(n))[0])
            return (u in l[0] and h in l[1]) or(u in l[1] and h in l[0])
        else:
            return False
    else:
        if n<10:
            return (n in l[0]) or (n in l[1]) or (n in l[2])
        elif n<100:
            u=n%10
            h=int((str(n))[0])
            return fun(n,[l[0],l[1]]) or fun(n,[l[0],l[2]]) or fun(n,[l[1],l[2]])
l=[]
n=int(input())
for i in range(n):
    l.append([int(j) for j in input().split()])
for i in range(1,100):
    if not(fun(i,l)):
        break
print(i-1)
            
            
        


from itertools import product as comb
from itertools import permutations as perm
k = int(input())
cubes = [input().split(" ") for i in range(k)]
nums = set()
for n in range(1, k + 1):
    for p in perm(list(range(k)), n):
#        print(p)
        # p is (0, 2, 1) order of cubes
        for c in comb(list(range(6)), repeat=n):
 #           if p == (1, 0):
#                print("--" + str(c))
            nn = ""
            for i in range(n):
  #              if p == (1, 0):
   #                 print("----" + str(p[i]) + "-" + str(c[i]) + "-" + cubes[p[i]][c[i]])
                nn += cubes[p[i]][c[i]]
            nums.add(int(nn))
#print("fuckwrvbwoe")
#print("yes" if 12 in nums else "no")
#print(nums)
ans = 0
while ans + 1 in nums:
    ans += 1
print(ans)


n = int(input())
cubes = []
for i in range(n):
    cubes.append(list(map(int, input().split())))

taken = []
for i in range(10000):
    taken.append(0)

res = 0
for cube in cubes:
    for face in cube:
            taken[face] += 1

for cube1 in cubes:
    for cube2 in cubes:
        if cube1 != cube2:
            for face1 in cube1:
                for face2 in cube2:
                    tmp = str(face1) + str(face2)
                    taken[int(tmp)] += 1

for cube1 in cubes:
    for cube2 in cubes:
        for cube3 in cubes:
            if cube1 != cube2 and cube1 != cube3 and cube2 != cube3:
                for face1 in cube1:
                    for face2 in cube2:
                        for face3 in cube3:
                            tmp = str(face1) + str(face2) + str(face3)
                            taken[int(tmp)] += 1

iterate = 0
for i in taken:
    if i == 0 and iterate > 0:
        break

    res = iterate
    iterate += 1
print(res)