#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int n, h;
vector<string> hint;
vector<bitset<10> > digit;
bitset<10> used;
string ret;

void solve(string& s, int i, int& num, vector<int>& hit0, vector<int>& blow0)
{
    if(i == n){
        ret = s;
        ++ num;
        return;
    }

    for(int j=0; j<10; ++j){
        if(used[j])
            continue;
        used[j] = true;
        s[i] = j + '0';

        vector<int> hit = hit0;
        vector<int> blow = blow0;
        bool ng = false;
        for(int k=0; k<h; ++k){
            if(s[i] == hint[k][i])
                -- hit[k];
            else if(digit[k][s[i]-'0'])
                -- blow[k];
            if(hit[k] < 0 || blow[k] < 0 || hit[k]+blow[k] >= n-i){
                ng = true;
                break;
            }
        }

        if(!ng)
            solve(s, i+1, num, hit, blow);

        used[j] = false;
        if(num == 2)
            break;
    }
}

int main()
{
    for(;;){
        cin >> n >> h;
        if(n == 0)
            return 0;

        hint.resize(h);
        digit.assign(h, 0);
        vector<int> hit(h), blow(h);
        for(int i=0; i<h; ++i){
            cin >> hint[i] >> hit[i] >> blow[i];
            for(int j=0; j<n; ++j)
                digit[i][hint[i][j] - '0'] = true;
        }

        string s(n, ' ');
        int num = 0;
        solve(s, 0, num, hit, blow);
        if(num == 1)
            cout << ret << endl;
        else
            cout << "NO" << endl;
    }
}