N, M = gets.chomp.split(' ').map(&:to_i)

X = []
Y = []
Z = []

conn = []

M.times { |i|
  X[i], Y[i], Z[i] = gets.chomp.split(' ').map(&:to_i)
  included = false
  conn.map { |c|
    if (c.include?(X[i]) || c.include?(Y[i]))
      c = (c | [X[i], Y[i]])
      included = true
    end
    c
  }
  conn.push([X[i], Y[i]]) if !included
}

print(conn.length)
