n,m = map(int,input().split())
n,m=int(n),int(m)
a=[input()for i in range(m)]
c=[]
g=0
for i in range(m):
    b=a[i]
    b=b.split()
    b=[int(j) for j in b]
    c.append(b)

if n>=2:
    for x in range(m):
        if c[x]==[1,0]:
            an=-1
        else:
            d=0
            e=0
            f=0
            for i in range(m):
                if c[i][0]==1:
                    if n==3:
                        d=10**2*c[i][1]
                    elif n==2:
                        e=10*c[i][1]
                    elif n==1:
                        f=c[i][1]
                    else:
                        g=1
                elif c[i][0]==2:
                    if n==3:
                        e=10*c[i][1]
                    elif n==2:
                        f=c[i][1]
                    else:
                        g=1
                elif c[i][0]==3:
                    f=c[i][1]
                else:
                    g=1
            an=d+e+f

else:
    for i in range(m):
        if c[i]==[1,0]:
            an=0
        else:
            d=0
            e=0
            f=0
            for i in range(m):
                if c[i][0]==1:
                    if n==3:
                        d=10**2*c[i][1]
                    elif n==2:
                        e=10*c[i][1]
                    elif n==1:
                        f=c[i][1]
                elif c[i][0]==2:
                    if n==3:
                        e=10*c[i][1]
                    elif n==2:
                        f=c[i][1]
                elif c[i][0]==3:
                    f=c[i][1]
            an=d+e+f


for i in range(m):
    for j in range(m):
        if c[i][0]==c[j][0] and c[i][1]!=c[j][1]:
           an=-1
#for i in range(m):
#    for j in range(m):
#        print(c[i])
#        print(c[j])
#        print(c[i][1],c[j][1])
#
#        if c[i][1]==c[j][1] and i!=j:
#
#           d.pop(j)
#print(c)
if g==1:
    print(-1)
elif g==0:
    print(an)
