class UnionFind
	def initialize
		@obj_to_node = {}
	end

	def add(obj)
		@obj_to_node[obj] ||= Node.new(obj)
	end

	def size(x = nil)
		x ? @obj_to_node[x].root.size : @obj_to_node.size
	end

	class Node
		def initialize(obj)
			@obj = obj
			@parent = self
			@size = 1
			@rank = 0
		end

		def root
			return self if @parent == self
			@parent = @parent.root
		end	

		attr_accessor :parent, :size, :rank
	end

	def union(x, y)
		u = (@obj_to_node[x] ||= Node.new(x)).root
		v = (@obj_to_node[y] ||= Node.new(y)).root
		return if u == v
		if u.rank > v.rank
			v.parent = u
			u.size += v.size
		elsif v.rank > u.rank
			u.parent = v
			v.size += u.size
		else
			u.parent = v
			v.rank += 1
			v.size += u.size
		end
	end

	def same?(x, y)
		return false unless u = @obj_to_node[x]
		return false unless v = @obj_to_node[y]
		u.root == v.root
	end
end

loop {
	n, m = gets.split.map &:to_i
	break if n == 0

	edges = (1..m).map{ gets.split.map &:to_i }.sort_by(&:last)
	min = Float::INFINITY

	until edges.empty?
		min_weight = edges[0][-1]
		uf = UnionFind.new
		edges.each{|i, j, w|
			uf.union(i, j)
			if uf.size(i) == n
				slim = w - min_weight
				min = slim if slim < min
				break
			end
		}

		edges.shift until edges.empty? || edges[0][-1] > min_weight
	end

	p min == Float::INFINITY ? -1 : min
}