def hoge(lst_f, tmp_f):
    s_f, c_f = map(int, input().split())
    if N > 1 and s_f == 1 and c_f == 0:
        print(-1)
        exit(0)
    for j_f in lst_f:
        try:
            if list(str(j_f))[s_f - 1] == str(c_f):
                tmp_f.append(j_f)
        except IndexError:
            pass
    return tmp_f


lst = []

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

s, c = map(int, input().split())
if N > 1 and s == 1 and c == 0:
    print(-1)
    exit(0)
for j in range(10 * (M - 1) if 10 * (M - 1) == 0 else 1, 1000):
    try:
        if list(str(j))[s - 1] == str(c):
            lst.append(j)
    except IndexError:
        pass
for i in range(M - 1):
    tmp = []
    lst = hoge(lst, tmp)

if not lst:
    print(-1)
else:
    print(lst[0])
