### subroutines

def count_rec(k, ds, hits, blows)
  if k >= $ln
    for i in (0...$hn)
      tri, hi, bi = $hints[i]
      return if hi != hits[i] || bi != blows[i]
    end

    $count += 1
    $ans = ds.join('')
    return
  end

  for d in (0..9)
    next if $used[d]

    hits0 = hits.clone
    blows0 = blows.clone
    skip = false

    for i in (0...$hn)
      tri, hi, bi = $hints[i]

      if tri[k] == d
        hits0[i] += 1
        if hits0[i] > hi
          skip = true
          break
        end
      elsif tri.include?(d)
        blows0[i] += 1
        if blows0[i] > bi
          skip = true
          break
        end
      end
    end
    next if skip

    $used[d] = true

    count_rec(k + 1, ds + [d], hits0, blows0)
    
    $used[d] = false

    return if $count > 1
  end
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
  $used = (0..9).map{false}

  count_rec(0, [], [0] * $hn, [0] * $hn)

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