N, M = gets.split.map(&:to_i)
x, y, z = M.times.map{gets.split.map(&:to_i)}.transpose
par = Array.new(N+1, 0)
mark = 1
for i in 0..(M-1)
  if z[i] % 2 == 0
    if par[x[i]] != 0
      par[y[i]] = par[x[i]]
    elsif par[y[i]] != 0
      par[x[i]] = par[y[i]]
    else
      par[x[i]] = par[y[i]] = mark
      mark += 1
    end
  else
    if par[x[i]] != 0
      par[y[i]] = par[x[i]]
    elsif par[y[i]] != 0
      par[x[i]] = par[y[i]]
    else
      par[x[i]] = mark
      par[y[i]] = mark + 1
      mark += 2
    end
  end
end
i = M-1
while i > 0
  if z[i] % 2 == 0
    if par[x[i]] != par[y[i]]
      maxm = [par[x[i]], par[y[i]]].max
      par[x[i]] = par[y[i]] = maxm
    end
  else
    maxn = [par[x[i]], par[y[i]]].max
    minn = [par[x[i]], par[y[i]]].min
    if maxn - minn != 1
      if par[x[i]] > par[y[i]]
        par[x[i]] = maxn
        par[y[i]] = minn
      else
        par[y[i]] = maxn
        par[x[i]] = minn
      end
    end
  end
  i -= 1
end
cnt = 0
cmb = Hash.new(0)
for i in 1..N
  if par[i] == 0
    cnt += 1
  else
    cmb[par[i]] += 1
  end
end
cmbs = 0
cmb.each do |k, v|
  cmbs += 1 if k > 0  
end
cmbs += (cmbs % 2 == 0 ? 0 : 1)
cmbs /= 2
puts cnt + cmbs