class Node
    attr_accessor :parent, :rank
    def initialize(n)
        @parent = n
        @rank = 0
    end
end

class UnionFindTree
    def initialize(n)
        @nodes = (0..n).to_a.map { |i| Node.new(i) }
    end
    def find(x)
        return x if @nodes[x].parent == x
        return @nodes[x].parent = find(@nodes[x].parent)
    end
    def unite(a, b)
        a = find(a)
        b = find(b)
        return if a == b
        if @nodes[a].rank < @nodes[b].rank
            @nodes[a].parent = b
        else
            @nodes[b].parent = a
            @nodes[b].rank += 1 if @nodes[a].rank == @nodes[b].rank
        end
    end
    def same?(a, b)
        find(a) == find(b)
    end
end
loop do
    n,m = gets.split.map(&:to_i)
    break if n == 0 && m == 0
    e = m.times.map{gets.split.map(&:to_i)}.sort{|a,b| a[2]<=>b[2]}
    min = Float::INFINITY
    e.size.times do |i|
        uf = UnionFindTree.new(n)
        count = n
        i.upto(m-1) do |j|
            if !uf.same?(e[j][0],e[j][1])
                uf.unite(e[j][0],e[j][1])
                count -= 1
                if count == 1
                    min = e[j][2] - e[i][2] if e[j][2] - e[i][2] < min
                    break
                end
            end
        end
    end
    p min != Float::INFINITY ? min : -1
end