### constant

MAX_W = 10000

### subroutines

def root(i)
  while i != $uft[i]
    i = $uft[i]
  end
  i
end

def spanning?()
  r0 = root(0)
  for i in (1...$n)
    return false if root(i) != r0
  end
  true
end

def check(k, rem, min_w, max_w)
  if rem == 0
    if spanning?()
      dw = max_w - min_w
      $min_dw = dw if $min_dw > dw
    end
    return
  end

  return if k >= $m

  # not use edges[k]
  check(k + 1, rem, min_w, max_w)

  # use edges[k]
  ak, bk, wk = $edges[k]

  ra = root(ak)
  rb = root(bk)
  $uft[rb] = ra

  min_w = wk if min_w > wk
  max_w = wk if max_w < wk
  check(k + 1, rem - 1, min_w, max_w)
  
  $uft[rb] = rb
end

### main

loop do
  $n, $m = gets.split.map(&:to_i)
  break if ($n | $m) == 0

  $edges = []
  $m.times do
    ai, bi, wi = gets.split.map(&:to_i)
    $edges << [ai - 1, bi - 1, wi]
  end
  #p $edges

  $uft = (0...$n).to_a
  $min_dw = MAX_W

  check(0, $n - 1, MAX_W, 0)

  puts ($min_dw >= MAX_W ? -1 : $min_dw)
end