import sys
N, M = map(int, input().split())

if N == 1:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append(c)
    if len(sc) == 0:
        print(0)
        sys.exit(0)
    elif len(sc) == 1:
        print(sc[0])
        sys.exit(0)
    else:
        print(-1)
        sys.exit(0)
        
if N == 2:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append([s, c])
    
    l1 = []
    for m in range(M):
        if sc[m][0] == 1 and sc[m][1] > 0:
            l1.append(sc[m][1])
    l2 = []
    for m in range(M):
        if sc[m][0] == 2:
            l2.append(sc[m][1])

    if len(l1) == 0 and len(l2) > 0:
        print(-1)
        sys.exit(0)
    elif len(l1) == 0 and len(l2) == 0:
        print(0)
        sys.exit(0)
    else:
        lank1 = min(l1)
        if len(l2) == 0:
            lank2 = 0
        else:
            lank2 = min(l2)
    
    ans = lank1 * 10 + lank2
    print(ans)
    sys.exit(0)
    
if N == 3:
    sc = []
    for m in range(M):
        s, c = map(int, input().split())
        sc.append([s, c])
        
    l1 = []
    for m in range(M):
        if sc[m][0] == 1 and sc[m][1] > 0:
            l1.append(sc[m][1])
    l2 = []
    for m in range(M):
        if sc[m][0] == 2:
            l2.append(sc[m][1])
    l3 = []
    for m in range(M):
        if sc[m][0] == 3:
            l3.append(sc[m][1])
    
    if len(l1) == 0:
        if len(l2) == len(l3) == 0:
            print(0)
            sys.exit(0)
        else:
            print(-1)
            sys.exit(0)
    else:
        lank1 = min(l1)
        if len(l2) == 0:
            lank2 = 0
        else:
            lank2 = 0
        if len(l3) == 0:
            lank3 = 0
        else:
            lank3 = min(l3)
    
    ans = lank1 * 100 + lank2 * 10 + lank3
    print(ans)
    sys.exit(0)