### 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_rec(k)
  return (MAX_W + 1) if k >= $m

  ak, bk, wk = $edges[k]
  ra = root(ak)
  rb = root(bk)

  uw = MAX_W + 1;

  if ra == rb
    uw = check_rec(k + 1)
  else
    $uft[rb] = ra

    if spanning?()
      uw = wk;
    else
      uw = check_rec(k + 1)
    end
    
    $uft[rb] = rb
  end

  uw
end

def check()
  min_dw = MAX_W + 1
  $uft = (0...$n).to_a

  for i in (0...$m)
    ai, bi, wi = $edges[i];
    lw = wi;
    uw = MAX_W + 1
    
    $uft[bi] = ai

    if spanning?()
      uw = wi
    else
      uw = check_rec(i + 1)
    end
    
    $uft[bi] = bi

    if uw <= MAX_W
      dw = uw - lw
      min_dw = dw if min_dw > dw
    end
  end
  
  min_dw
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
  $edges.sort!{|a, b| a[2] <=> b[2]}
  #p $edges

  min_dw = check()

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