class quickfind:
  def __init__(self, n):
    self.i2g = [ i for i in range(n) ]
    self.g2i = [ [i] for i in range(n) ]
  
  def size(self, x):
    return len(self.g2i[self.i2g[x]])
  
  def parent(self, x):
    return self.i2g[x]
  
  def merge(self, l, r):
    l = self.parent(l)
    r = self.parent(r)
    if l == r:
      return False
    if self.size(l) < self.size(r):
      l, r = r, l
    for v in self.g2i[r]:
      self.i2g[v] = l
      self.g2i[l].append(v)
    self.g2i[r] = []
    return True
  
N, M = map(int, input().split())
E = []
for i in range(M):
  a, b, c = map(int, input().split())
  a -= 1
  b -= 1
  E.append((a, b, c))
E = sorted(E, key=lambda t:t[2])
qf = quickfind(N)
ans = 0
for id, e in enumerate(E):
  if qf.size(a) == N:
    ans = M - id
    break
  qf.merge(e[0], e[1])
print(ans)  
