← Home
package main

import (
	"bufio"
	"fmt"
	"os"
)

type Edge struct {
	to int
	d  int
}

func main() {
	in := bufio.NewReaderSize(os.Stdin, 1<<20)
	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	defer out.Flush()

	var n, m int
	if _, err := fmt.Fscan(in, &n, &m); err != nil {
		return
	}

	adj := make([][]Edge, n)
	for i := 0; i < m; i++ {
		var a, b, c int
		fmt.Fscan(in, &a, &b, &c)
		a--
		b--
		d := 1 ^ c
		adj[a] = append(adj[a], Edge{b, d})
		adj[b] = append(adj[b], Edge{a, d})
	}

	color := make([]int, n)
	for i := 0; i < n; i++ {
		color[i] = -1
	}

	ans := make([]int, 0)

	for i := 0; i < n; i++ {
		if color[i] != -1 {
			continue
		}

		queue := []int{i}
		color[i] = 0
		comp0 := make([]int, 0)
		comp1 := make([]int, 0)

		for head := 0; head < len(queue); head++ {
			v := queue[head]
			if color[v] == 0 {
				comp0 = append(comp0, v)
			} else {
				comp1 = append(comp1, v)
			}

			for _, e := range adj[v] {
				need := color[v] ^ e.d
				if color[e.to] == -1 {
					color[e.to] = need
					queue = append(queue, e.to)
				} else if color[e.to] != need {
					fmt.Fprintln(out, "Impossible")
					return
				}
			}
		}

		if len(comp1) <= len(comp0) {
			for _, v := range comp1 {
				ans = append(ans, v+1)
			}
		} else {
			for _, v := range comp0 {
				ans = append(ans, v+1)
			}
		}
	}

	fmt.Fprintln(out, len(ans))
	for i, v := range ans {
		if i > 0 {
			fmt.Fprint(out, " ")
		}
		fmt.Fprint(out, v)
	}
	fmt.Fprintln(out)
}