### subroutines

def max_dist(u, d, path)
  finished = true

  for v in $nbrs[u]
    duv, ei = $edges[u][v]
    next if $used[ei]

    finished = false
    $used[ei] = true
    max_dist(v, d + duv, path + [v])
    $used[ei] = false
  end

  if finished
    if $max_dist < d
      $max_dist = d
      $max_path = path.clone
    elsif $max_dist == d && ($max_path <=> path) > 0
      $max_path = path.clone
    end
  end
end

### main

loop do
  ns, nl = gets.split.map(&:to_i)
  break if (ns | nl) == 0
  #p [ns, nl]

  $edges = ns.times.map{[]}
  $nbrs = ns.times.map{[]}

  for i in (0...nl)
    s0i, s1i, di = gets.split.map(&:to_i)
    s0i -= 1
    s1i -= 1
    $edges[s0i][s1i] = $edges[s1i][s0i] = [di, i]
    $nbrs[s0i] << s1i
    $nbrs[s1i] << s0i
  end
  #p $edges
  #p $nbrs

  $max_dist = 0
  $max_path = nil

  for i in (0...ns)
    $used = nl.times.map{false}
    max_dist(i, 0, [i])
  end
  #p [$max_dist, $max_path]

  puts $max_dist
  puts $max_path.map{|i| i + 1}.join(' ')
end