def solve(a, b, w, n, graph)
  v = [a, b]
  c = [w]
  while v.size < n
    min_dif, min_v, cost = INF, INF, INF
    v.each do |u|
      graph[u].each do |to, w|
        next if v.include?(to)
        if min_dif > (dif = [(c.max - w).abs, (c.min - w).abs].min)
          min_dif = dif
          min_v = to
          cost = w
        end
      end
    end
    return -1 if cost == INF
    v << min_v
    c << cost
  end
  c.max - c.min
end

INF = 1 << 29
loop do
  n, m = gets.split.map(&:to_i)
  break if n == 0
  if m == 0
    puts -1
    next
  end
  costs = m.times.map{ gets.split.map(&:to_i) }.map{ |a, b, w| [a - 1, b - 1, w] }
  graph = n.times.map { [] }
  costs.each{ |a, b, w| graph[a] << [b, w]; graph[b] << [a, w] }
  puts costs.map{ |a, b, w| solve(a, b, w, n, graph) }.min
end