import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Main{

    /** ??????????????? */
    private static String DELIMITER = " ";
    /** ?§£???????????? */
    private static List<String> answers = new ArrayList<>();
    /** ???????????? */
    private static String NO_ANSWER = "NO";
    /** ?§£???????????° */
    private static int existAnswer = 0;

    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String firstLine = null;
        while ((firstLine = reader.readLine()) != null && !"0 0".equals(firstLine)) {
            Header header = createHeader(firstLine.split(DELIMITER));
            searchAnswer(reader, header);

            // System.out.println(result);

            if (!(existAnswer == 1)) {
                answers.add(NO_ANSWER);
            }
            /** ????????? */
            existAnswer = 0;
        }

        for (String answer : answers) {
            System.out.println(answer);
        }
    }

    /**
     * ?????????
     * 
     * @param reader
     * @param header
     * @throws IOException
     */
    public static void searchAnswer(BufferedReader reader, Header header) throws IOException {
        List<HintSet> hints = createHintSets(reader, header.hintNumber);
        boolean[] usedNumber = getUsedNumbers(hints, header.digit);
        int[] answer = new int[header.digit];
        Arrays.fill(answer, -1);

        for (int i = 0; i < 10; i++) {
            if (usedNumber[i]) {
                continue;
            }

            executeRoundRobin(0, hints, header.digit, answer, i, usedNumber);
        }

    }

    /**
     * ?????? ????????????????´¢?????????
     * 
     * @param currentDigit
     *            ?????¨??????
     * @param hints
     *            ??????????????????
     * @param digit
     *            ????????°
     * @param answer
     *            ?§£???
     * @param next
     *            ?¬????
     * @param usedNumber
     */
    public static void executeRoundRobin(int currentDigit, List<HintSet> hints, int digit, int[] answer, int next,
            boolean[] usedNumber) {

        // ????????????????????£???????????´?????????????????????
        if (existAnswer >= 2) {
            return;
        }

        String result = NO_ANSWER;

        // ?????????????????´???????????¨???????????°?????????????????????????????????????????????????????????
        if (currentDigit == digit - 1) {

            answer[currentDigit] = next;
            // ???????????????????????????????????´????????????????????????
            boolean match = true;

            for (HintSet hint : hints) {
                if (!hitAndBlow(hint, answer)) {
                    match = false;
                    break;
                }
            }

            // ??????????????¨
            // for (int number : answer) {
            // System.out.print(number);
            // }
            // System.out.println();

            if (match) {
                if (!(existAnswer >= 1)) {
                    result = createResult(answer);
                    answers.add(result);
                } else {
                    answers.remove(answers.size() - 1);
                }
                existAnswer++;
            }
            return;
        }

        answer[currentDigit] = next;
        usedNumber[next] = true;

        // ??????????????¨
        // for (int number : answer) {
        // System.out.print(number);
        // }
        // System.out.println();
        for (int i = 0; i < 10; i++) {
            if (usedNumber[i]) {
                continue;
            }

            executeRoundRobin(currentDigit + 1, hints, digit, answer, i, usedNumber);
        }

        answer[currentDigit] = -1;
        usedNumber[next] = false;
        return;
    }

    /**
     * ?????? ??°????????????????????????????????????
     * 
     * @param answer
     * @return ?§£???
     */
    public static String createResult(int[] answer) {
        StringBuilder builder = new StringBuilder();
        for (int i : answer) {
            builder.append(i);
        }

        return builder.toString();
    }

    /**
     * ?????? ???????????¢??????????????????????????????
     * 
     * @param hint
     *            ??????????????????
     * @param answer
     *            ?§£???
     * @return
     */
    public static boolean hitAndBlow(HintSet hint, int[] answer) {
        int hit = 0;
        int blow = 0;

        for (int i = 0; i < answer.length; i++) {

            if (hint.number[i] == answer[i]) {
                hit++;
                continue;
            }

            for (int j = 0; j < answer.length; j++) {
                if (hint.number[i] == answer[j]) {
                    blow++;
                    break;
                }
            }
        }

        return hint.hit == hit && hint.blow == blow;
    }

    /**
     * ?????? ??????????????????????????????
     * 
     * @param firstLine
     * @return ?????????????????????
     */
    public static Header createHeader(String[] firstLine) {

        /** ????????° */
        int digit = Integer.parseInt(firstLine[0]);
        /** ???????????° */
        int hintCount = Integer.parseInt(firstLine[1]);

        return new Header(digit, hintCount);
    }

    /**
     * ?????? ????????????????????????????????????
     * 
     * @param reader
     *            ?¨??????\???
     * @param hintCount
     *            ???????????°
     * @return ??????????????????
     * @throws IOException
     */
    public static List<HintSet> createHintSets(BufferedReader reader, int hintCount) throws IOException {
        ArrayList<HintSet> hints = new ArrayList<>();
        for (int i = 0; i < hintCount; i++) {
            hints.add(createHint(reader.readLine().split(DELIMITER)));
        }

        return hints;
    }

    /**
     * ?????? ???????????????????????????
     * 
     * @param line
     * @return
     */
    private static HintSet createHint(String[] line) {

        String[] numberStr = line[0].split("");
        int[] number = new int[line[0].length()];
        for (int i = 0; i < line[0].length(); i++) {
            number[i] = Integer.parseInt(numberStr[i]);
        }

        int hit = Integer.parseInt(line[1]);

        int blow = Integer.parseInt(line[2]);

        return new HintSet(number, hit, blow);
    }

    /**
     * ????????? ??°??????0~9???????????¨?????????????????????????´????????????????????????? ??¬?????? ?????????????´????true?????´?????????????????????????????°???????????¨????????¨?????????
     * 
     * @param hints
     *            ??????????????????
     * @param digit
     *            ?????°
     * @return ??°???????????¨????????????????´?????????????????????¨????????????true???
     */
    public static boolean[] getUsedNumbers(List<HintSet> hints, int digit) {
        boolean[] usedNumber = new boolean[10];
        if (digit != 10) {
            for (HintSet hintSet : hints) {
                if (hintSet.hit + hintSet.blow == digit) {

                    for (int i = 0; i < 10; i++) {
                        boolean check = false;
                        for (int j = 0; j < digit; j++) {
                            if (hintSet.number[j] == i) {
                                check = true;
                                break;
                            }
                        }
                        if (!check) {
                            usedNumber[i] = true;
                        }
                    }
                    break;
                }
            }

        }
        return usedNumber;
    }
}

/**
 * 
 * ?????????????????±????´??????????
 *
 */
class Header {
    /** ?????° */
    int digit;
    /** ?????????????????° */
    int hintNumber;

    public Header(int digit, int hintNumber) {
        super();
        this.digit = digit;
        this.hintNumber = hintNumber;
    }

}

/**
 * 
 * ???????????????????????????
 *
 */
class HintSet {
    /** ???????????¨??°????????? */
    int[] number;
    /** ???????????° */
    int hit;
    /** ???????????° */
    int blow;

    public HintSet(int[] number, int hit, int blow) {
        super();
        this.number = number;
        this.hit = hit;
        this.blow = blow;
    }

}

Measured memory_bytes: 40340