import sys
input = sys.stdin.readline
def main():
    N, M = map(int, input().split())
    m = {}
    for i in range(1, 4):
        m[i] = set()
    for _ in range(M):
        s, c = map(int, input().split())
        m[s].add(c)
    exists_ans = True
    for i in range(1, N):
        if len(m[i]) > 1:
            exists_ans = False
            break
    if (N == 2 and (0 in m[2])) or (N == 3 and (0 in m[1])):exists_ans = False
    if exists_ans:
        ans = 0
        for i in range(1, N+1):
            if len(m[i]) > 0:
                num = m[i].pop() * (10**(3-i))
                ans += num
    else:
        ans = -1
    print(ans)
if __name__ == "__main__":
    main()