#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) 
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }


int L, H;
vector< pair<string,pint> > data;
vector<set<char> > se;
bool used[10];
int con = 0;
string res;

void dfs(int v, string str, vector<pint> now) {
    //cout << v << ", " << str << ", " << now << endl;
    if (v == L) {
        bool ok = true;
        for (int i = 0; i < now.size(); ++i) {
            if (now[i] != data[i].second) ok = false;
        }
        if (ok) {
            ++con; res = str;
        }
        return;
    }
    
    for (char c = '0'; c <= '9'; ++c) {
        if (used[c-'0']) continue;
        
        string nstr = str + c;
        vector<pint> next = now;
        bool ok = true;
        for (int i = 0; i < now.size(); ++i) {
            if (data[i].first[v] == c) { ++next[i].first; }
            else {
                if ( se[i].count(c) ) { ++next[i].second; }
            }
            
            if (next[i].first > data[i].second.first) { ok = false; break; }
            if (next[i].second > data[i].second.second) { ok = false; break; }
        }
        if (ok) {
            used[c-'0'] = true;
            dfs(v+1, nstr, next);
            used[c-'0'] = false;
        }
    }
}

int main() {
    // freopen( "/Users/macuser/Dropbox/Contest/input.in", "r", stdin );
    
    while (cin >> L >> H) {
        if (L == 0) break;
        
        data.resize(H);
        se.clear(); se.resize(H);
        
        for (int i = 0; i < H; ++i) {
            string str; int h; int b;
            cin >> str >> h >> b;
            data[i] = MP(str,pint(h,b));
            for (int j = 0; j < str.size(); ++j) se[i].insert(str[j]);
        }
        
        memset(used, 0, sizeof(used));
        res = "";
        string str = "";
        con = 0;
        vector<pint> now(H, pint(0,0));
        
        dfs(0, str, now);
        
        if (con == 1) cout << res << endl;
        else cout << "NO" << endl;
    }
    return 0;
}


Measured memory_bytes: 1232