def uftInit()
  0.upto(5000).each{ |i| $UFT[i] = i }
end

def root(x)
  $UFT[x] == x ? x : $UFT[x] = root($UFT[x])
end

def unite(x, y)
  $UFT[root(x)] = root(y)
end

def connected?(i, j)
  root(i) == root(j)
end

def spanningTree?(n)
  1.upto(n) do |i|
    return false if root(i) != root(1)
  end
  true
end

def total(g, n, m)
  min   = 10001
  max   = -1
  total = 10000

  0.upto(m-1) do |rmenum|
    uftInit()

    g[rmenum..-1].each do |c, i, j|
      unless connected?(i, j)
        max = c if c > max
        min = c if c < min
        unite(i, j)
      end
    end

    total = (max-min < total) ? max-min : total if spanningTree?(n)
  end

  total == 10000 ? -1 : total
end

if __FILE__ == $0
  $UFT = Array.new(5000)

  loop do
    n, m = gets.split.map(&:to_i)
    break if n.zero? and m.zero?

    # [i, j, c]から[c, i, j]に置き換え、コスト順にソートする.
    graph = m.times.map{ gets.split.map(&:to_i) }.map!{ |i, j, c| [c, i, j] }.sort.reverse
    
    p total(graph, n, m)
  end    
end