fun main(args:Array<String>){
    val (n, m) = readLine()!!.split(" ").map { it.toInt() }
    val list1 = mutableListOf<Int>()
    val list2 = mutableListOf<Int>()
    for(i in 0 until m) {
        val (s,c) = readLine()!!.split(" ").map{it.toInt()}
        list1.add(s)
        list2.add(c)
    }
    var tmp1 = 0
    var tmp2 = 0
    var tmp3 = 0
    var isClear = true
    for(i in 0 until m) {
        if(list1[i] == n-2) {
            if(tmp1 == 0 || tmp1 == list2[i] * 100) {
                tmp1 = list2[i] * 100
            } else {
                isClear = false
                break
            }
        } else if(list1[i] == n-1)  {
            if(tmp2 == 0 || tmp2 == list2[i] * 10) {
                tmp2 = list2[i] * 10
            } else {
                isClear = false
                break
            }
        } else {
            if(tmp3 == 0 || tmp3 == list2[i]) {
                tmp3 = list2[i]
            } else {
                isClear = false
                break
            }
        }
    }
    if(isClear && ((n == 3 && tmp1 !=  0) || (n == 2 && tmp2 != 0) || (n == 1))) {
        println(tmp1+tmp2+tmp3)
    } else {
        println(-1)
    }
}