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)

var ans = -1
var strans = "";
if(N == 1):
    if(table.hasKey(1)):
        echo table[1]
    else:
        echo "0"

elif(N == 2):
    if(table.hasKey(2)):
        if(not table.hasKey(1)):
            table.add(1, 0)
        
        echo $table[1] & $table[2]

    else:
        echo "-1"

elif(N==3):
    if(table.hasKey(3)):
        if(not table.hasKey(2)):
            table.add(2, 0)
        if(not table.hasKey(1)):
            table.add(1, 0)
        
        echo $table[1] & $table[2] & $table[3]
    else:
        echo "-1"

