### constant

INF = 1 << 30

### subroutines

def transform(login)
  trans = {}
  len = login.length
  
  # 1. Delete
  for i in (0...len)
    v = login.clone
    v[i] = ''
    trans[v] = true
  end

  # 2. Insert
  for i in (0..len)
    for ch in ('a'..'z')
      v = login.clone
      v[i, 0] = ch
      trans[v] = true
    end
  end

  # 3. Replace
  for i in (0...len)
    for ch in ('a'..'z')
      next if login[i] == ch
      v = login.clone
      v[i] = ch
      trans[v] = true
    end
  end

  # 4. Swap
  (0...len).each_cons(2) do |i, j|
    next if login[i] == login[j]
    v = login.clone
    v[i] = login[j]
    v[j] = login[i]
    trans[v] = true
  end

  trans
end

### main

loop do
  n = gets.to_i
  break if n == 0

  d = gets.to_i

  logins = n.times.map{gets.strip}
  #p logins

  ltrs = {}
  logins.each{|l| ltrs[l] = transform(l)}
  
  clogins = []

  for i in (0...n)
    li = logins[i]
    likeys = ltrs[li].keys

    for j in ((i + 1)...n)
      lj = logins[j]

      # d == 1
      if ltrs[li][lj] || ltrs[lj][li]
        clogins << [li, lj].sort
        next
      end
      next if d < 2
      
      # d == 2
      for lii in likeys
        if ltrs[lj][lii]
          clogins << [li, lj].sort
          break
        end
      end
    end
  end

  clogins.sort.each do |l0, l1|
    puts "#{l0},#{l1}"
  end
  puts clogins.length
end