n,m = list(map(int,input().split()))
nlist = []
clist = []
for i in range (m):
    s,c = list(map(int,input().split()))
    nlist.append(s)
    clist.append(c)
if m == 0:
    print(0)
else:
    for i in range (len(nlist)-1):
        for j in range (i+1, len(nlist)):
            if nlist[i] == nlist[j]:
                if clist[i] != clist[j]:
                    print(-1)
                    exit()

    a = 1
    b = 0
    c = 0

    for i in range (len(nlist)):
        if nlist[i] == 1:
            a = (clist[i])
        if nlist[i] == 2:
            b = (clist[i])
        if nlist[i] == 3:
            c = (clist[i])

    if n == 1:
        print(a)
    elif n == 2:
        if a == 0:
            print(-1)
        else:
            print(a,end = "")
            print(b)
    elif n == 3:
        if a == 0:
            print(-1)
        else:
            print(a,end = "")
            print(b,end = "")
            print(c)