import * as fs from 'fs';

//in
// 3 3
// 1 7
// 3 2
// 1 7
//out
// 702
const main = (inp: string) => {
    let inpArray: string[] = inp.split('\n');
    const [n, m]: number[] = inpArray[0].split(' ').map(Number);
    inpArray.shift();

    let ans1: number = 10;
    let ans2: number = 10;
    let ans3: number = 10;
    for (let i = 0; i < m; i++) {
        const [s, c]: number[] = inpArray[i].split(' ').map(Number);

        if (s === 1) {
            if (ans1 > c) {
                ans1 = c;
            }
        }

        if (s === 2) {
            if (ans2 > c) {
                ans2 = c;
            }
        }

        if (s === 3) {
            if (ans3 > c) {
                ans3 = c;
            }
        }

    }

    if (ans1 === 10) {
        ans1 = 0;
    }
    if (ans2 === 10) {
        ans2 = 0;
    }
    if (ans3 === 10) {
        ans3 = 0;
    }

    if (ans1 === 0) {
        console.log(-1);
    } else if (ans1 === 0 && ans2 === 0) {
        console.log(-1);
    } else if  (ans1 === 0 && ans2 === 0 && ans3 === 0) {
        console.log(-1);
    } else {
        console.log(ans1.toString() + ans2.toString() + ans3.toString());
    }


};

main(fs.readFileSync('/dev/stdin', 'utf8').trim());
