import strutils, sequtils, math, algorithm, tables

# let
#     # 文字列
#     s = stdin.readLine

#     # 単一数値
#     n = stdin.readLine.parseInt

#     # 数列
#     a = stdin.readLine.split.map(parseInt)

# 複数の数値の分割代入
var
    N, M: int
(N, M) = stdin.readLine.split.map(parseInt)

var table = initTable[int, int]()

for i in 0..<M:
    var
        s1, c1: int
    (s1, c1) = stdin.readLine.split.map(parseInt)
    if not table.hasKey(s1):
        table.add(s1, c1)
    else:
        table[s1] = min(table[s1], c1)


if(N == 1):
    if(not table.hasKey(1)):
        table.add(1, 0)
    for i in 0..9:
        if(i == table[1]):
            echo i
            quit()
    echo "-1"
    quit()

if(N == 2):
    if(not table.hasKey(2)):
        table.add(2, 0)
    if(not table.hasKey(1)):
        table.add(1, 0)

    if(table[1] != 0):
        echo $table[1] & $table[2]
    else:
        echo "-1"
        quit()

if(N == 3):
    if(not table.hasKey(3)):
        table.add(3, 0)
    if(not table.hasKey(2)):
        table.add(2, 0)
    if(not table.hasKey(1)):
        table.add(1, 0)

    if(table[1] != 0):
        echo $table[1] & $table[2] & $table[3]
    else:
        echo "-1"
        quit()



