### subroutines

def check_try(ns, tns)
  htn = bln = 0

  for i in (0...$ln)
    ti = tns[i]
    if ti == ns[i]
      htn += 1
    elsif ns.include?(ti)
      bln += 1
    end
  end

  [htn, bln]
end

### main

loop do
  $ln, $hn = gets.split.map(&:to_i)
  break if ($ln | $hn) == 0

  hints = []

  $hn.times do
    try, hit, blow = gets.strip.split
    tns = try.split('').map(&:to_i)
    htn = hit.to_i
    bln = blow.to_i
    hints << [tns, htn, bln]
  end
  #p hints

  count = 0
  ans = nil

  (0..9).to_a.permutation($ln) do |ns|
    match = true

    for hint in hints
      htns, hhtn, hbln = hint
      htn, bln = check_try(ns, htns)
      if htn != hhtn || bln != hbln
        match = false
        break
      end
    end

    if match
      count += 1
      ans = ns.join('')
    end
  end

  puts (count != 1 ? 'NO' : ans)
end