{
    N=$1;M=$2;
    for(;i++<M;){
        getline;
        S[i]=$1;C[i]=$2;
    }
    for(;a++<10^N&&b<1;){
        b=a-1;
        for(i=0;i++<M;)b*=length(b)==N&&substr(b,S[i],1)==C[i];
    }
    print a<10^N?b:-1;
}