
class UnionFind
  def initialize(n)
    @par = Array.new(n)
    n.times do |i|
      @par[i] = i
    end
  end
  def find(x)
    return @par[x] = (@par[x] == x ? x : find(@par[x]))
  end
  def unite(x,y)
    unless find(x) == find(y)
      @par[find(x)] = find(y)
    end
  end
end
loop do
  n, m = gets.split.map(&:to_i)
  break if n == 0
  ans, edges = [], []
  m.times do
    edges <<= gets.split.map(&:to_i)
  end
  edges.sort!{|a, b| a[2] <=> b[2]}
  m.times do |i|
    uf = UnionFind.new(n + 1)
    cnt, last = 0, 0
    i.upto(m-1) do |j|
      unless uf.find(edges[j][0]) == uf.find(edges[j][1])
        uf.unite(edges[j][0], edges[j][1])
        last = edges[j][2]
        cnt += 1
      end
    end
    if cnt == n - 1
      ans <<= last - edges[i][2]
    end
  end
  if ans.empty?
    puts -1
  else
    puts ans.min
  end
end