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