//01
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int l,h;
vector<string> tr;
vector<int> hi,bl;
char t[11];
string a;
int ac;

void dfs(int d,int u){
  if(d!=l){
    for(int i=0;i<10;i++){
      if(!(u>>i&1)){
	t[d]='0'+i;
	for(int i=0;i<tr.size();i++){
	  int ch=0,cb=0;
	  for(int j=0;j<=d;j++){
	    if(tr[i].find(t[j])!=string::npos){
	      ((t[j]==tr[i][j])?ch:cb)++;
	    }
	  }
	  if(ch+l-d-1<hi[i]||ch>hi[i]||cb+l-d-1<bl[i]||cb>bl[i])goto next;
	}
	dfs(d+1,u|1<<i);
      next:
	;
      }
    }
  }else{
    t[d]=0;
    ac++;
    a=t;
  }
}

int main(){
  while(cin>>l>>h,l|h){
    tr.resize(h);
    hi.resize(h);
    bl.resize(h);
    while(h--){
      cin>>tr[h]>>hi[h]>>bl[h];
    }
    ac=0;
    dfs(0,0);
    if(ac==1){
      cout<<a<<endl;
    }else{
      cout<<"NO"<<endl;
    }
  }
  return 0;
}