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;
    let ans1Cnt: number = 0;
    let ans2Cnt: number = 0;
    let ans3Cnt: number = 0;
    for (let i = 0; i < m; i++) {
        const [s, c]: number[] = inpArray[i].split(' ').map(Number);

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

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

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

    if (ans1Cnt > 1 || ans2Cnt > 1 || ans3Cnt > 1) {
        console.log(-1);
        return;
    }

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

    const ans11: string = ans1 === 0 ? '' : ans1.toString();
    const ans22: string = ans11 === '' && ans2 === 0 ? '' : ans2.toString();
    if (n === 3) {
        if (ans1 === 0 && ans2 === 0 && ans3 === 0) {
            console.log('000');
        } else{
            console.log(ans11 + ans22 + ans3.toString());
        }
    }
    if (n === 2) {
        if (ans1 === 0 && ans2 === 0) {
            console.log('00');
        } else{
            console.log(ans11 + ans22);
        }
    }
    if (n === 1) {
        if (ans1 === 0) {
            console.log(0);
        } else {
            console.log(ans11);
        }
    }
};

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